如何添加类别preFIX到log4net的消息?类别、消息、preFIX、log4net

2023-09-03 10:30:55 作者:如约而至

我想补充类preFIX所有的留言上现有的日志消息。然而,它是乏味添加这个preFIX所有现有的日志消息一个接一个。有没有一种方式,我可以只添加属性到类的水平,那么在这个类中的所有邮件将被记录为某些类别?

而不是方式现在如下,

  Log.Info([参考] 1级开始时...);
 

我真的想这样的事,或用不同的方式来定义log4net.ILog。

  [LoggingCategory(参考)
公共类MyClass的
{
   公共无效的MyMethod()
   {
        Log.Info(1级开始时...);
   }
}
 

解决方案

有趣的问题,粗糙的尝试......

Log4NetLogger - 日志适配器

 公共类Log4NetLogger
{
    私人只读的ILog _logger;
    私人只读字符串_category;

    公共Log4NetLogger(类型类型)
    {
        _logger = LogManager.GetLogger(类型);
        _category =的getCategory();
    }

    私人字符串的getCategory()
    {
        VAR属性=新的StackFrame(2).GetMethod()DeclaringType.GetCustomAttributes(typeof运算(LoggingCategoryAttribute),假的)。
        如果(attributes.Length == 1)
        {
            变种ATTR =(LoggingCategoryAttribute)属性[0];
            返回attr.Category;
        }
        返回的String.Empty;
    }

    公共无效调试(字符串消息)
    {
        如果(_logger.IsDebugEnabled)_logger.Debug(的String.Format([{0}] {1},_category,消息));
    }
}
 
Log4Net 日志记录

LoggingCategoryAttribute - 适用于类

  [AttributeUsage(AttributeTargets.Class)
公共类LoggingCategoryAttribute:属性
{
    私人只读字符串_category;

    公共LoggingCategoryAttribute(串类)
    {
        _category =类别;
    }

    公共字符串等级{{返回_category; }}
}
 

LogTester - 一个测试实施

  [LoggingCategory(LT)
公共类LogTester
{
    私人静态只读Log4NetLogger记录器=新Log4NetLogger(typeof运算(LogTester));

    公共无效测试()
    {
        Logger.Debug(该日志消息应该有一个prepended类);
    }
}
 

I like to add category prefix to all the messages on the existing logging messages. However it is tedious to add this prefix to all the existing logging messages one by one. Is there a way in I can just add an attribute to the class level then all the messages in this class will be logged for certain category?

Instead of the way right now as below,

Log.Info("[Ref] Level 1 Starts ...");

I really want to something like this or a different way to define the log4net.ILog.

[LoggingCategory("Ref")]
public class MyClass 
{
   public void MyMethod()
   {
        Log.Info("Level 1 Starts ...");
   }
}

解决方案

Interesting problem, rough attempt...

Log4NetLogger - logging adapter

public class Log4NetLogger
{
    private readonly ILog _logger;
    private readonly string _category;

    public Log4NetLogger(Type type)
    {
        _logger = LogManager.GetLogger(type);
        _category = GetCategory();
    }

    private string GetCategory()
    {
        var attributes = new StackFrame(2).GetMethod().DeclaringType.GetCustomAttributes(typeof(LoggingCategoryAttribute), false);
        if (attributes.Length == 1)
        {
            var attr = (LoggingCategoryAttribute)attributes[0];
            return attr.Category;
        }
        return string.Empty;
    }

    public void Debug(string message)
    {
        if(_logger.IsDebugEnabled) _logger.Debug(string.Format("[{0}] {1}", _category, message));
    }
}

LoggingCategoryAttribute - applicable to classes

[AttributeUsage(AttributeTargets.Class)]
public class LoggingCategoryAttribute : Attribute
{
    private readonly string _category;

    public LoggingCategoryAttribute(string category)
    {
        _category = category;
    }

    public string Category { get { return _category; } }
}

LogTester - a test implementation

[LoggingCategory("LT")]
public class LogTester
{
    private static readonly Log4NetLogger Logger = new Log4NetLogger(typeof(LogTester));

    public void Test()
    {
        Logger.Debug("This log message should have a prepended category");
    }
}