如何使用与log4net的包装类时记录的方法名 [英] how to log method name when using wrapper class with Log4net

查看:294
本文介绍了如何使用与log4net的包装类时记录的方法名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现自定义的XML格式的log4.net

I am implementing a custom XML formatter for log4.net

public class ISDSApplicationEventsLayout : XmlLayoutBase
{
    protected override void FormatXml(...)
    {
        //Location Info 
        writer.WriteStartElement("Method");
        writer.WriteString(**loggingEvent.LocationInformation.MethodName * *);
        writer.WriteEndElement();
    }
}

问题是...现在,当我调用日志方法,从我的日志包装类...称为记录

The problem is ... now when I call log method from my log wrapper class... called logging

public static void logEvent(string message)
{
    log.Info(isdsLog); 
}

我得到的输出....

I get the output....

  <Method>logEvent</Method>

怎么可能有这种所谓的LOGEVENT,而不是LOGEVENT作为方法名,方法名?

How is it possible to have the method name that called logEvent, rather than logEvent as the method name?

感谢您

提问时间:

如果这个上面似乎有点复杂 - 我所要问的是:你如何保持这种所谓的log4net的...

If this above seems a bit complicated - what I am really asking is : How do you keep the context of the method that called the wrapping logging function in log4net...

例如...方法的doWork()...调用 - >日志包装 - >调用log4net的......

example... method doWork()... calls -> logging wrapper --> calls log4net....

如何做才能让你的方法名=的doWork和不记录的包装功能......

How do you make the methodname = doWork and NOT logging wrapper function....

推荐答案

其实,你可以用出位的现成的log4net很容易地解决这个问题。您的包装可以拨打Logger.Log并通过您的包装类的类型作为第一个参数。所以,你的包装看起来是这样的:

Actually, you can fix this easily with out-of-the-box log4net. Your wrapper can call Logger.Log and pass the type of your wrapper class as the first parameter. So, your wrapper might look something like this:

public class MyLog4NetWrapper
{
  ILog log = LogManager.GetLogger("WhateverYourLoggerNameIs");

  public void logEvent(string message) 
  {     
    log.Logger.Log(typeof(MyLog4NetWrapper), LogLevel.Info, message, null);
  } 
}

在log4net的记录一条消息,它遍历调用堆栈,直到它到达其声明类型等于传递的记录方法的第一个参数类型的方法。下一个方法了堆栈是实际调用点。

When log4net logs a message, it traverses up the call stack until it gets to the method whose declaring type is equal to the type passed in as the first parameter of the Log method. The next method up the stack is the actual call site.

至于包裹log4net的记录,我不知道,我会建议创建一个静态的包装类。与主要的问题是,你只能在你的app.config文件的单一配置的记录器。换句话说,你将无法从您的code不同部分独立控制的日志记录。如果你有A类和B类,并都使用静态包记录器,那么这两个类将记录在同一水平。如果你想打开登录A类,灭B类,你就不能这样做。

As far as wrapping the log4net logger, I'm not sure that I would recommend creating a static wrapper class. The main problem with that is that you can only have a single configurable logger in your app.config file. In other words, you won't be able to independently control the logging from different parts of your code. If you have class A and class B and both use your static wrapped logger, then both classes will log at the same level. If you wanted to turn logging on for class A and off for class B, you would not be able to do so.

这篇关于如何使用与log4net的包装类时记录的方法名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆