使用包装器时,如何保留 Log4Net 记录的类和方法名称?

发布于 2024-08-17 09:31:38 字数 602 浏览 4 评论 0原文

我需要一个 Log4net 包装器 - 以便暴露于大型应用程序中的许多不同组件。我显然想在记录时保留类和方法名称,但我会避免将类型等传递给我的包装器。

我查看了这个问题,它与我的非常相似,但这没有帮助。

我已经在 这个其他问题中看到它与 smt 类似以下:

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

这并不理想,因为它没有使用开箱即用的 Log4Net 功能。

在我偏离主题并想出一些非常复杂的东西之前,我想先了解人们是如何做到这一点的。任何指示(链接/资源/示例)表示赞赏!

I need a Log4net wrapper - to be exposed to a number of different components in a large app. I obviously want to retain the class and method name when logging but I would keep away of passing down type etc to my wrapper.

I had a look at this question which is very similar to mine, but it didn't help.

I've seen it done in this other question with smt like the following:

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

This is not ideal since it's not using the out-of-the-box Log4Net functionality.

I'd like to get an idea of how people are doing this before I go for the tangent and come up with something very complicated. Any pointers (links/resources/samples) appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事未完 2024-08-24 09:31:38

Log4net 允许您访问方法名称,例如%method。这不是最快的操作,但如果您需要调试某些内容,则可以很好地使用它。我相信您的问题是如果您使用包装器,log4net 将不会输出正确的方法名称。

要解决这个问题,您必须查看 log4net 如何编写 ILog 实现。这基本上是内部 log4net Logger 的包装,因此同样的问题也适用于 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);

显然还有一些事情要做,但重点已经提到了。

Log4net allows you to access method name for instance like this %method. This is not the fastest operation but if you need to debug something you may very well use it. I believe your question is about the fact that log4net will not output the correct method name if you use a wrapper.

To solve that problem you have to look at how log4net wrote the ILog implementation. This is basically a wrapper around the internal log4net Logger and therefore the very same problem applies to the ILog implementation, too. I did basically the following:

// 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);

Obviously there are some things to do yet, but the important points are mentioned.

农村范ル 2024-08-24 09:31:38

像这样创建你的包装类...

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

Create your wrapper class like this...

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);
        }
    }

Configure your log4net.config file in the following manner...

<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>

Now just use above these methods like this...

// 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
北陌 2024-08-24 09:31:38

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

MethodBase.GetCurrentMethod().Name

您总是可以为 Log4Net 创建一个包装器,它接受您需要的任何参数(如 MethodBase)?

I usually add something this... (it's all the functionality I need)

MethodBase.GetCurrentMethod().Name

you could always create a wrapper for Log4Net that takes any params you need (like MethodBase)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文