0

I am using NLog for some logging and thanks to these answers: Getting Logger Name into Excel file with NLog https://stackoverflow.com/questions/50123661/nlog-in-c-sharp-with-severity-and-categories?noredirect=1#comment87344565_50123661

I am able to log different types of events (ex: "Thermal", "Database",etc.) to a single file with the logger field showing the type of event. One simply calls for example:

NLog.LogManager.GetLogger("Database").Debug("Error writing to DB");

This all works fine and might be enough. However, you'll notice that any programmer is free to put any name they want in GetLogger AND misspell it. "GetLogger("Datobuse"). It would be nice if the programmer had to choose from an enum or other structure:

NLog.LogManager.GetLogger(LoggerNames.Database).Debug("Error writing to DB");

This seems like it might be a common problem and might already have an elegant solution. I can imagine overriding the LogManager class but am not sure of the specifics. Note that LogManager is a public static class in the NLog library so not clear how to hide it. Also, there is the nice property that if you fill in the config file once in your app project, the config file works perfectly for all the projects in the solution as long as you include NLog as a reference.

I'll go down the path of creating a library project that makes use of NLog library and then include that it my main project UNLESS there is already a great solution. I'm betting there is, but haven't seen one.

Thanks,

Dave

Dave
  • 8,095
  • 14
  • 56
  • 99
  • Just implement your own version of the LogManager with a method that accepts an enum and calls the real version. Then require your developers to use your custom manager. As for forbidding calls to the "real" log manager: https://softwareengineering.stackexchange.com/questions/358951/forbid-calls-to-arbitrary-functions-classes-in-external-code –  May 16 '18 at 14:42

1 Answers1

0

If you have "global" logger names, then the easy solution is just to use global logger instances.

The same global NLog Logger instance can be used by multiple locations without getting into threading issues.

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Yes I do have "global" logger names such as "Thermal" or "StateMachine" depending on nature of problem. But I'm not sure what you mean. Could you give a few more details? Do you mean do all the GetLogger calls once at the top level project and save the Logger objects? How would that stop another project from calling GetLogger("StatChaning") Notice mispelled logger name. My understanding is that I can have just one Nlog.config file at topmost project BUT I need to add reference to NLog to each project. Once everyone has a reference to NLog they can call GetLogger("AnyNameYouWant") – Dave May 16 '18 at 20:30
  • Instead of having a shared project that contains the magical enum-contants. Then you could just have a shared project that contains the magical global-logger-instances. Only the shared-project will call GetLogger("Thermal") everyone else will get the logger from the shared project. Anyway sounds like you are shooting with big cannons to avoid magical readonly string-values. Be careful with spending too much time trying to protect yourself from yourself. – Rolf Kristensen May 16 '18 at 22:00
  • Yes anyone in your project can call `GetLogger("AnyNameYouWant")`. If you have a large application with many modules, then you usually use namespaces to avoid name-clashes. If you want to have an isolated NLog-configuration (instead of a global one), then you can create your own local LogFactory. See also https://github.com/NLog/NLog/wiki/Configure-component-logging – Rolf Kristensen May 21 '18 at 17:58