如何包装log4net的时候登录方法名? [英] How to log MethodName when wrapping Log4net?

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

问题描述

我在一个静态的包装包裹log4net的,并要记录

  loggingEvent.LocationInformation.MethodName
loggingEvent.LocationInformation.ClassName
 

但我得到的是我的包装的名称。

使用forwardingappender和像

静态包装类我如何登录该信息

  Logger.Debug(记录到调试);
Logger.Info(记录到信息);
Logger.Warn(记录为警告);
Logger.Error(前);
Logger.Fatal(前);
 

解决方案

那么错误是什么地方在我的附加目的地,但出于完整性病包括答案尽我所知:

您需要应该换ILogger和NOT的ILog门面

 公共静态类记录仪
 {
    私人只读静态类型ThisDeclaringType = typeof运算(记录器);
    私人静态只读ILogger defaultLogger;

    静态记录仪()
    {
      defaultLogger =
        LoggerManager.GetLogger(Assembly.GetCallingAssembly(),MyDefaultLoggger);
 

...

 公共静态无效信息(字符串消息)
    {
        如果(defaultLogger.IsEnabledFor(infoLevel))
        {
            defaultLogger.Log(typeof运算(记录器),infoLevel,信息,空);
        }
    }
 

I have wrapped Log4net in a static wrapper and want to log

loggingEvent.LocationInformation.MethodName
loggingEvent.LocationInformation.ClassName

However all I get is the name of my wrapper.

How can I log that info using a forwardingappender and a static wrapper class like

Logger.Debug("Logging to Debug");
Logger.Info("Logging to Info");
Logger.Warn("Logging to Warn");
Logger.Error(ex);
Logger.Fatal(ex);

解决方案

Well the error was somewhere in my appender but for completeness ill include the answer to the best of my knowledge:

the Facade you need should wrap ILogger and NOT ILog

 public static class Logger
 {
    private readonly static Type ThisDeclaringType = typeof(Logger);
    private static readonly ILogger defaultLogger;

    static Logger()
    {
      defaultLogger =
        LoggerManager.GetLogger(Assembly.GetCallingAssembly(),"MyDefaultLoggger");

...

    public static void Info(string message)
    {
        if (defaultLogger.IsEnabledFor(infoLevel))
        {
            defaultLogger.Log(typeof(Logger), infoLevel, message, null);
        }
    }

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

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