سؤال

I have created a generic implementation ILogger and implemented NLog implementation methods inside this wrapper. My question is i have enabled Error severity for entire namspace in my solution but also i wanted Info level diagnostics for classes under a "Employee" namespace so i have another rule which sets classes under this namespace qualify for Info level logging. But however when i log only error and fatal gets logged for classes under these namespace. When i debug i can see that the IsInfoEnabled() comes false for the logger instance for these classes ? I feel they way i have configured my wrapper class seems to be faulty ? Can someone advice.

generic ILogger interface public interface ILogger {

    bool Trace(string message, params String[] args);


    bool Info(string message, params String[] args);


    bool Error(string message, params String[] args);


    bool Fatal(string message, params String[] args);


    bool Error(string message, Exception ex, String[] args);


    bool Fatal(string message, Exception ex, params String[] args);
}

implementation of ILogger interface

 using NLog;

 class NLogImpl :  ILogger
 {  

     NLog.Logger logger = null; 


    public NLogImpl(string source)
    {
        logger = LogManager.GetLogger(source);
    }        


    bool ILogger.Trace(string message ,params String[] args)
    {

        logger.Trace(BuildArgumentsWithMessage(message , args));
        return true;
    }...

Class using the generic ILogger this class logs only error and fatal message but not any other level even though in the config file i have enabled Info this namespace

  public class MainTechDetails
   {
      private static ILogger logger = CommonLogger.GetLogger("MainTechDetails");

      public void LogMainTechDetails()
       {
         logger.Trace("Sample trace message");
         logger.Info("Sample informational message");
         logger.Error("Sample error message");
         logger.Fatal("Sample fatal error message");
        }
    }

I have one target and 2 rules for this target

    <target xsi:type="File"
            name="logFile"
            fileName="D:\LogInformation.log"
            layout="${date} ${callsite} ${message}"/>
    </targets>

    <rules>
     <logger name="CustomLoggingNlog.Employee.*" minlevel="Info" writeTo="logFile" />
     <logger name="*" minlevel="Error" writeTo="logFile"/>    
    </rules>
هل كانت مفيدة؟

المحلول

It looks like you have a few problems:

  1. You have configured logging rules for "CustomLoggingNLog.Employee." and "". In your sample application you retrieve a logger called "MainTechDetails". You should retrieve the logger based on the fully namespace qualified name: CustomLoggingNLog.Employee.MainTechDetails. Or, if you generic implementation allows it, by using the type:

    CommonLogger.GetLogger(typeof(MainTechDetails));

  2. Your wrapper, as written, will not maintain the call site correctly (if you use NLog's call site LayoutRenderer). Rather than getting the call site of your code where you are calling your logger, you will get the call site from inside of your logger. That is probably not very useful. See my answer in this question for an example of how to wrap NLog such that the call site is preserved:

Nlog Callsite is wrong when wrapper is used

Good luck!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top