2

I have a Logger class like this:

public class Logger : ILogging
{
    private ILogger _logger;

    public Logger()
    {
        _logger = LoggerFactory<ILogging>.Resolve();
    }

    public void log(string priority, string message)
    {
  //to do the code here
    }

}

and this is my config file:

 <rules >
<logger name="Priority1" minlevel="Error" writeTo="logfile" />
<logger name="Priority2" minlevel="Warn" writeTo="logfile" />
<logger name="Priority3" minlevel="Debug" writeTo="logfile" />
<logger name="Priority4" minlevel="Trace" writeTo="logfile" />

I want by "Name" rules to write to a specific function in nlog for example: if user call

 logger.log("Priority1","errorMessage");

the function know to go for logger.error("error")

I tried to see in google but I didnt see any nice solve

UPDATE

      public void log( Priority priority, string message)
    {
        string currentLogLevel;
        _logger = NLog.LogManager.GetLogger(priority.ToString());
        LogEventInfo logEvent = new LogEventInfo(currentLogLevel , _logger.Name, message);
        _logger.Log(logEvent);
    }
Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
  • Do I see this correctly: All of those write to the same logfile? So why bother splitting the loggers? Just use the loglevel as it is intended. I also don't see the benefit of the Wrapper class. – Fildor Mar 22 '18 at 10:45
  • yes I want to print alI in the same file ,I dont want to print all the priority levels in the file,for example: I want by bother splitting the loggers to decide what to print and what not, etc: debug,trace-not important to print and error I want to print becouse it is important – user3580768 Mar 22 '18 at 10:51
  • there is any idea??? – user3580768 Mar 22 '18 at 11:14
  • That's exactly what [rules](https://github.com/nlog/nlog/wiki/Configuration-file#rules) are for in NLog. You can explicitly set `level`, `levels`, `minlevel` and `maxlevel` SO if you set legger "*" to minlevel "Error" then all logs with loglevel Error and higher will be written to target, the others not. I have the feeling that you are depriving yourself from that functionality by introducing your wrapper. – Fildor Mar 22 '18 at 11:56
  • ^^ That doesn't mean you shouldn't have a wrapper at all, but I guess you need to reconsider its architecture. – Fildor Mar 22 '18 at 12:01
  • I update my question : I want to decided by the rule which level I will put in logEventInfo see my update – user3580768 Mar 22 '18 at 12:08
  • Yes, that doesn't make sense. What _is_ `currentLogLevel`? You can have 1 single rule if you pass the correct loglevel for the logmessage to be logged in. You just need to map priority to loglevel. – Fildor Mar 22 '18 at 12:16
  • exactly I want to map the level from this rule, how should I do this?? – user3580768 Mar 22 '18 at 12:29

2 Answers2

0

You can manage a wrapper class (factory),

See example in:

How to retain callsite information when wrapping NLog

user1012506
  • 2,048
  • 1
  • 26
  • 45
0

Following the comments to OP:

Instead of

public void log( Priority priority, string message)
{
    string currentLogLevel; // <= This can't work
    _logger = NLog.LogManager.GetLogger(priority.ToString());
    LogEventInfo logEvent = new LogEventInfo(currentLogLevel , _logger.Name, message);
    _logger.Log(logEvent);
}

do

public void log( Priority priority, string message)
{
    LogLevel logLevel;

    switch(priority)
    {
        case Priority.Priority1 : loglevel = LogLevel.Error; break;
        case Priority.Priority2 : loglevel = LogLevel.Warn; break;
        case Priority.Priority3 : loglevel = LogLevel.Debug; break;
        case Priority.Priority4 : loglevel = LogLevel.Trace; break;
        default: loglevel = LogLevel.None; break;
    }

    _logger = NLog.LogManager.GetLogger(priority.ToString());
    LogEventInfo logEvent = new LogEventInfo(logLevel , _logger.Name, message);
    _logger.Log(logEvent);
}

for a start.

But I also suggest to then use only one logger and have that as a static field of your wrapper.

I personally use NLog without wrapper, usually. That gives me the freedom to get class-based loggers and specialized loggers that I can route and filter in the config file. But if you need or want to use a wrapper, I'd think about passing some context along with the message if you are not doing that in the message itself.

Fildor
  • 14,510
  • 4
  • 35
  • 67