Log4Net:如何向每个日志行添加简单索引器(行号)

发布于 2024-09-26 14:42:28 字数 176 浏览 4 评论 0原文

问题是这样的: 我想将行索引器(行号或迭代器)添加到日志文件中的每一行。 我不需要它对于整个日志文件是唯一的,但仅对于“应用程序会话”(即每次 Win32 应用程序启动时,我们开始倒计时:1、2、3 等)

是任何内置的 -以某种方式(conversionpattern 标记语法)或某些自定义扩展?

多谢!

Here is the problem:
I want to add row indexer (row number or iterator) to each row in log file.
I don't need it to be unique for the whole log file, but only for the "application session" (i. e. each time Win32 application started, we begin our countdown: 1, 2, 3 and so on)

Is where any built-in way (conversionpattern tag syntax) or some custom extension?

Thanks a lot!

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

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

发布评论

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

评论(2

毁我热情 2024-10-03 14:42:28

我的解决方案仅适用于一个附加程序,每次启动应用程序时计数器都会重置。

您可以使用模式转换器:

public sealed class LineCounterPatternConverter : PatternLayoutConverter
{       
    private static int _counter = 0;

    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var counter = System.Threading.Interlocked.Increment(ref _counter);
        writer.Write(counter.ToString());
    }
}

然后像这样配置它:

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="[%LC] %message%newline" />
  <converter>
    <name value="LC" />
    <type value="YourNameSpace.LineCounterPatternConverter" />
  </converter>
</layout>

[] 括号当然是可选的。您还可以执行类似 %6LC 的操作,该操作会用空格填充该行,以便使消息很好地对齐。

My solution only works for one appender only and the counter will be reset every time you start the application.

You could use a pattern converter:

public sealed class LineCounterPatternConverter : PatternLayoutConverter
{       
    private static int _counter = 0;

    protected override void Convert(TextWriter writer, LoggingEvent loggingEvent)
    {
        var counter = System.Threading.Interlocked.Increment(ref _counter);
        writer.Write(counter.ToString());
    }
}

then you configure it like this:

<layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="[%LC] %message%newline" />
  <converter>
    <name value="LC" />
    <type value="YourNameSpace.LineCounterPatternConverter" />
  </converter>
</layout>

The [] brackets are of course optional. You could also do something like this %6LC which would pad the line with spaces so that you get the message aligned nicely.

南城追梦 2024-10-03 14:42:28

我会执行以下操作:

首先,定义一些日志记录助手

public class WebLoggingHelper
{
    public static CurrentLineHelper CurrentLine = new CurrentLineHelper();

    public class CurrentLineHelper
    {
        private long _counter = 0;

        public override string ToString()
        {
            long counter = System.Threading.Interlocked.Increment(ref _counter);
            return counter.ToString();
        }
    }
}

然后将日志记录助手连接到初始化记录器位置的 log4net 属性

public void ConfigureLog4Net()
{
    XmlConfigurator.Configure();
    GlobalContext.Properties["CurrentLine"] = WebLoggingHelper.CurrentLine;
}

利润!或者更严重的是,现在您可以使用 %property{CurrentLine} 直接在 log4net 格式中使用该属性,

<conversionPattern value="%property{CurrentLine}- %date - %identity - %property{current_request_id} - %m%n"/>

我使用此方法来记录大量需要计算和特定于应用程序的内容。
您可能需要添加一些逻辑以在很长的时间内使用长数字,并对行编号进行一些格式化(例如在左侧填充零等)...

良好的记录!


编辑:
我在此提出的解决方案一次只能用于一个日志文件。如果您在许多日志文件中使用此模式,则每个if中都会有一个唯一的行号,如果您添加了针对竞争条件的保护(只需在 tostring 方法中添加一个 synclock辅助对象的)
要使用行号管理多个记录器,应使用另一种方法

EDIT#2:
更新为线程安全,还更新为与已接受的答案共享标识符名称,以方便读者(更容易消化和比较。)

i'd do the following:

First, define some logging helper

public class WebLoggingHelper
{
    public static CurrentLineHelper CurrentLine = new CurrentLineHelper();

    public class CurrentLineHelper
    {
        private long _counter = 0;

        public override string ToString()
        {
            long counter = System.Threading.Interlocked.Increment(ref _counter);
            return counter.ToString();
        }
    }
}

Then wire the logging helper to the log4net properties in the place where you initialize your logger

public void ConfigureLog4Net()
{
    XmlConfigurator.Configure();
    GlobalContext.Properties["CurrentLine"] = WebLoggingHelper.CurrentLine;
}

Profit! or more seriously, now you can user the property directly in your log4net formatting by using %property{CurrentLine}

<conversionPattern value="%property{CurrentLine}- %date - %identity - %property{current_request_id} - %m%n"/>

I use this method to log lots of stuff that needs to be calculated and application specific.
You may want to add some logic to use the long number for very long periods, and some formatting to the line numbering (padding with zeroes on the left for example, etc)...

Good logging!


EDIT:
The solution i present here must be used only for one logging file at a time. I you use this pattern in many log files, you'd have a unique line number in each if you add protection against race conditions (just add a synclock in the tostring method of the helper object)
To manage multiple loggers with line number, another method should be used

EDIT#2:
Updated to be thread safe, also updated to share identifier names with accepted answer for benefit of readers (easier to digest and compare.)

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