log4net的写入单个文件每次调用log.info [英] log4net write single file for each call to log.info

查看:214
本文介绍了log4net的写入单个文件每次调用log.info的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用log4net的捕捉过程中运行我的应用程序,这是正常使用中发生的任何异常,然后,我创建了自己的静态方法,它负责捕获失败的XML对象,它的目的是要进一步提供阅读恭维日志条目。

I have been using log4net to capture any exceptions which occur during my applications running, which is working perfectly, I then created my own static Method which was responsible for capturing the failed XML object which was intended to provide further reading to compliment the log entry.

public static void WriteReceiptToXml(Exception ex, string XmlReceipt)
{
    if (XmlReceipt != null)
    {
        string filename = ConfigurationManager.AppSettings["XmlReceiptPath"] + "/ReceiptXml" + DateTime.Now.ToString("ddMMyyhhmmss") + ".xml";
        File.WriteAllText(filename, XmlReceipt);
        Logger.Error(string.Format("The Xml Receipt was written to {0}", filename));
    }

    else
        Logger.Error("The Xml Receipt was empty so no document was created. Please reference the Payment request XML");
}

这效果不错,但是一个更有经验的同事认为,这是一个code气味升技和我是有效地重新发明轮子,因为我已经使用log4net的这个任务应当由它太处理

This worked well, however a more experienced colleague suggested that this was abit of a code smell and that I was effectively re-inventing the wheel, as I am already using log4net this task should be handled by it too.

在这一点上,我创建在Web.config文件中(如下图)一个新的附加器和过滤它在信息层面,我这时才意识到的是,当我运行应用程序,log4net的将不分创建XML文件是否catch块捕获该异常,并会保留为空白,如果没有发生错误,这显然不是我想要的。

At this point I created a new appender in the Web.config file (below) and filtered it on the INFO level, what I realised then was that as soon as I ran the application, log4net would create the XML file regardless of whether the catch block caught the exception and would leave it empty if no error occurred, which is obviously not what I want.

<appender name="ReceiptXmlAppender" type="log4net.Appender.FileAppender" >
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <file type="log4net.Util.PatternString" value="App_Data/ReceiptXml/Receipt-%date{yyyy-MM-dd_HH-mm-ss}.xml"/>
  <appendToFile value="false" />
  <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%m" />
  </layout>
  <filter type="log4net.Filter.LevelRangeFilter">
    <param name="LevelMin" value="INFO"/>
    <param name="LevelMax" value="INFO"/>
  </filter>
</appender>

总之我要的是:

In summary what I want is:

  • log4net的仅创建于INFO方法的XML被称为我的catch块
  • 1文件被创建,每次我打电话LOG.INFO
  • 在该文件必须加盖日期时间
  • 我需要知道创建的XML文档的文件名,这样我可以追加这实际日志文件作为补充,堆栈跟踪。

推荐答案

如果您使用的基本 FileAppender 或其衍生物你的运气之一;该文件一旦附加目的地在 ActivateOptions初始化()方法创建的。从我看见你需要一个自定义FileAppender将处理所有业务规则。这将需要

If you use the basic FileAppender or one of its derivatives you are out of luck; the file is created as soon as the appender is initialized in the ActivateOptions() method. From what I see you need a custom FileAppender that will handle all your business rules. It would need to

  • 覆盖默认 ActivateOptions 方法prevent文件创建
  • 覆盖追加方法来创建一个新的时间戳的文件时记录一条消息
  • LoggingEvent所类(您可在类中的一些属性)来检索文件名通过使用数据;您之前记录。以确定文件名
  • override the default ActivateOptions method to prevent file creation
  • override the Append method to create a new timestamped file when a message is logged
  • use data passed in the LoggingEvent class (you have some properties available on the class) to retrieve the filename; you have to determine the filename before logging.

除非你想从它的行为已经实现受益(模拟为例),我建议你跳过从 FileAppender 类继承和直接从<$ C继承$ C> TextWriterAppender 类。

Unless you want to benefit from behavior it already implements(impersonation for example), I'd recommend skipping inheritance from the FileAppender class and inherit directly from the TextWriterAppender class.

这篇关于log4net的写入单个文件每次调用log.info的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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