Log4Net写入不同的文件 [英] Log4Net Writing to different files

查看:305
本文介绍了Log4Net写入不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个车辆跟踪应用程序(ASP.NET MVC C#).我有Windows服务,可接收GPS设备发送的数据.在该服务中,我编写了用于记录数据的代码.

I'm making a vehicle tracking application(ASP.NET MVC C#). I have windows service that takes data sent by GPS device. In the service I have written the code to Log the data.

现在考虑一种正常的日志记录方案,其中我只有一个GPS设备.

Now consider a normal logging scenario in which i have only one GPS device.

08:00:24内部OnDataAvailable方法

08:00:24 Inside OnDataAvailable Method

08:00:25收到数据-设备ID:2个数据:abcdefghijkl

08:00:25 Data Received - Device Id: 2 Data: abcdefghijkl

08:00:25保留OnDataAvailable

08:00:25 Leaving OnDataAvailable

和其他一些语句.然后重复

and few more statements. and then it repeats

现在,当我有一个以上的GPS设备正在发送数据时,日志会混杂在一起.那就是我的以下日志:

Now when I have more than one GPS device sending data, the log gets mixed. That is I have following kind of log:

08:00:23内部OnDataAvailable方法08:00:24内部OnDataAvailable 方法

08:00:23 Inside OnDataAvailable Method 08:00:24 Inside OnDataAvailable Method

08:00:25收到数据-设备ID:2个数据:abcdefghijkl

08:00:25 Data Received - Device Id: 2 Data: abcdefghijkl

08:00:25保留OnDataAvailable

08:00:25 Leaving OnDataAvailable

08:00:26收到数据-设备ID:1个数据:abcdefghijkl

08:00:26 Data Received - Device Id: 1 Data: abcdefghijkl

08:00:26保留OnDataAvailable

08:00:26 Leaving OnDataAvailable

现在我要实现的是,对于不同的设备,我应该具有不同的日志文件.因此,对于ID为1的设备,我有Log_D1.txt,对于ID为2的设备,我有Log_D2.txt.

Now what I want to achieve is that, I should have different log files for different devices. So for device with Id 1 I have Log_D1.txt, For Device Id 2, Log_D2.txt.

如果有人能指出我正确的方向,将不胜感激.

Would appreciate if someone can point me in the right direction.

推荐答案

您应该能够使用上下文对象之一(例如ThreadContext.Properties)来构建文件名.您可以配置日志文件名称以使用属性来建立名称.因此,在您的情况下,您可以将设备ID存储在上下文中(您的请求是否在单独的线程中处理?).

You should be able to use one of the context objects (like ThreadContext.Properties) to build the filename. You can configure the log file name to use properties to build up the name. So, in your case, you might store the device id in the context (are your requests being handled in separate threads?).

本文对此进行了很好的解释:

This article has a good explanation of how this works:

http://geekswithblogs. net/rgupta/archive/2009/03/03/dynamic-log-filenames-with-log4net.aspx

总而言之,您可以对附加器进行如下配置:

To summarize, you can configure your appender something like this:

<appender name="RollingFileAppenderV1" type="log4net.Appender.RollingFileAppender">
     <file type="log4net.Util.PatternString" value="F:\HornetFeed\Log_%property{DeviceID}" />
     <appendToFile value="true" />
     <rollingStyle value="Size" />
     <maxSizeRollBackups value="-1" />
     <maximumFileSize value="5000KB" />
     <staticLogFileName value="true" />
     <countDirection value="1"/>
     <layout type="log4net.Layout.PatternLayout">
         <conversionPattern value="%m%n" />
     </layout>
     <filter type="log4net.Filter.PropertyFilter">
         <Key value="Version" />
         <StringToMatch value="1" />
     </filter>
     <filter type="log4net.Filter.DenyAllFilter" />
</appender>

请注意在文件名规范中使用%property.

Note the use of %property in the filename specification.

然后,在您的代码中,您可以执行以下操作:

Then, in your code, you might do something like this:

private void OnDataAvailable(string id, int data)
{
  ThreadContext.Properties["DeviceID"] = id;

  logger.Info("Inside OnDataAvailable");

  logger.Info("Leaving OnDataAvailable");
}

所有日志消息,至少在设置了DeviceID属性的情况下,都应隔离到单独的文件中,并通过DeviceID进行部分命名.

All log messages, at least in cases where the DeviceID property has been set, should be segregated into separated files, named, in part, by the DeviceID.

这篇关于Log4Net写入不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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