This repository has been archived by the owner on Dec 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Logging
Marc Wittke edited this page Oct 15, 2018
·
2 revisions
To use the Backend.Fx logging abstraction from any class you can just create a private static logger:
private static readonly Backend.Fx.Logging.ILogger Logger = Backend.Fx.Logging.LogManager.Create<MyClassThatShouldLogSomething>();
and start using the field throughout your code like this:
Logger.Trace("This is a huge trace message, expensive to collect and to construct, and must be requested even in a developer environment actively by changing the logging configuration");
Logger.Debug("Oh, well this is interesting when hunting bugs, but a productive system won't log this regularly");
Logger.Info("I am informing about the normal program flow");
Logger.Warn("This should not occur, but whatever, it don't hurt me (the backend). Probably it was a bad request.");
Logger.Error(caughtException, "Ouch, something went wrong. I better log the whole exception es well.");
Logger.Fatal(caughtException, "What the ...? I am dying!");
- CONSIDER logging at least one info per (public) method.
- DO Log every swallowed exception
- DON'T log private or even secure data. Logging Id values is okay, while logging a birth date is not. Logging a password kills a kitten. 😢
- CONSIDER to log an exception you are throwing. If you decide to do so, DO log the exception with a low severity, since it will be caught and logged by higher levels. The
ILogger
interface provides a convenient method:
throw Logger.Debug(new InvalidOperationException("Someone was doing something wrong"));
- However, DON'T use this convenience method to log exceptions you are re-throwing. This will break the stack trace and you might be unable to find the bug you are hunting.
catch (Exception ex)
{
// bad code, DON'T do this
throw Logger.Debug(ex, "Someone was doing something wrong");
// this is what you must do
Logger.Debug(ex, "Someone was doing something wrong");
throw;
}
- DO use the logger like a stopwatch
using (Logger.InfoDuration("uploading"))
{
// we're interested in how long it might take
}
- DO check whether the log level is enabled, when doing expensive logging work on low severity
if (Logger.IsDebugEnabled())
{
// construct and log a huge log message
}