2010-01-12 60 views
7

我需要一个Log4net包装 - 在大型应用程序中暴露给许多不同的组件。我显然希望在记录时保留类和方法的名称,但我会避免将类型等传递给我的包装。使用包装器时,如何保留Log4Net的日志类和方法名?

我看过this question,这与我的非常相似,但没有帮助。

我已经看到了它在this other question完成通过类似以下的SMT:

MethodBase methodBase = new StackTrace().GetFrame(1).GetMethod(); 
this.log.Debug(methodBase.Name + " : " + message); 

这因为它不使用外的开箱log4net的功能是不理想的。

我想知道人们在做切线之前如何做这件事,并想出一些非常复杂的东西。任何指针(链接/资源/样本)赞赏!

回答

2

我通常添加这个东西......(这是我需要的所有功能)

MethodBase.GetCurrentMethod().Name 

你总是可以创建一个log4net的包装,把你需要的任何PARAMS(如MethodBase)?

+1

谢谢 - 我想避免传递类型和methodName。 – JohnIdol 2010-01-15 12:21:43

4

Log4net允许你像这个%方法那样访问方法名。这不是最快的操作,但如果你需要调试一些东西,你可能会很好地使用它。我相信你的问题是,如果你使用包装,log4net将不会输出正确的方法名称。

要解决这个问题,你必须看看log4net如何编写ILog实现。这基本上是内部log4net记录器的一个包装,因此同样的问题也适用于ILog实现。我做了基本如下:

// define a field such as this 
private static readonly Type ThisDeclaringType = typeof(MyLogWrapper); 

// in constructor. Note: I use the internal Logger! 
this.Logger = LogManager.GetLogger(name).Logger; 

// I created a WriteLog method that calls the internal logger like this 
// you will need to translate to the internal log levels 
// message and ex are the items I want to log (ex can be null) 
this.Logger.Log(MyLogWrapper.ThisDeclaringType, log4netLevel, message, ex); 

显然有一些事情要做呢,但提到的要点。

3

创建包装类,像这样...

public static class LogFourNet 
    { 
     // Define a static logger variable so that it references the 
     private static readonly ILog Log = LogManager.GetLogger(typeof(LogFourNet)); 

     static LogFourNet() 
     { 
      XmlConfigurator.Configure(
        new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config")); 
      Log.Info("Log4net is configured."); 
     } 

     public static void Info(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0, 
      [CallerFilePath] string caller = "", [CallerMemberName] string memberName = "") 
     { 
      Log.Info("[" + currentObj.GetType().Namespace + "." + 
        Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg); 
     } 

     public static void Debug(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0, 
      [CallerFilePath] string caller = "", [CallerMemberName] string memberName = "") 
     { 
      Log.Debug("[" + currentObj.GetType().Namespace + "." + 
         Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg); 
     } 

     public static void Error(object currentObj, string msg, [CallerLineNumber] int lineNumber = 0, 
      [CallerFilePath] string caller = "", [CallerMemberName] string memberName = "") 
     { 
      Log.Error("[" + currentObj.GetType().Namespace + "." + 
         Path.GetFileNameWithoutExtension(caller) + "." + memberName + ":" + lineNumber + "] - " + msg); 
     } 
    } 

配置以下列方式你log4net.config文件...

<configuration> 
    <configSections> 
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> 
    </configSections> 
    <log4net> 
    <appender name="FileAppender" type="log4net.Appender.FileAppender"> 
     <file value="logfile.log" /> 
     <appendToFile value="true" /> 
     <layout type="log4net.Layout.PatternLayout"> 
     <conversionPattern value="%d [%t] %-5p - %m%n" /> 
     </layout> 
    </appender> 
    <root> 
     <!--LogLevel: OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL --> 
     <level value="ALL" /> 
     <appender-ref ref="FileAppender" /> 
    </root> 
    </log4net> 
</configuration> 

现在只需使用上述这些方法...

// need to pass this(current obj) as we want to log full class name 
LogFourNet.Debug(this, "started something from wrapper"); 

output: 
------- 
2017-02-04 15:38:37,549 [1] DEBUG [WebAPI_DI.Startup.Configuration:25] - started something from wrapper