Log4net动态命名xml文件,存储并使用路径和名称追加log.txt

时间:2014-05-08 15:05:59

标签: c# .net log4net

我正在使用log4net来捕获try catch块中的任何错误。像这样:

      catch (Exception ex)
        {
            //theres been an error and xmlReceipt is not null
            if (xmlReceipt != null)
            {
                //Log the error
                Logger.Error(ex);
                //log the Receipt object to an xml file in App_Data/ReceiptXml
                Logger.Error(xmlReceipt);
                //Append log.txt, explain where the ReceiptXml can be found
                Logger.Error(string.Format("The Xml Receipt was written to {0}", ConfigurationManager.AppSettings["XmlReceiptPath"]));

            }

            //theres been an error and xmlReceipt is null
            else
                Logger.Error(ex);
                Logger.Error("The Xml Receipt was empty so no document was written");                               

            throw;    

        }

我想要实现的是,当检测到错误并且xmlReceipt对象不是null时,请在log.txt中记录错误,这可以追加,以便随着时间的推移而增长。然后我将相同的log.txt附加到路径中,最后我希望使用xmlReceipt对象创建一个xml文档。忽略else

请参阅下面的web.config,我曾尝试动态创建一个新的xml文档,每次在名称中都有一个动态日期,但实际发生的是使用名称Receipt.xml创建的xml文档没有日期,里面的xml也包含堆栈跟踪。

任何人都可以帮我改变web.config,以便动态地将日期添加到文件名中,而不包括堆栈跟踪,只包括xmlReceipt对象。如果您还可以建议我如何将带有动态日期的xml文件路径添加到IF行中的string.Format语句,这将是很好的,因为我可以在log.txt中建议确切的名称。与错误日志相关的xml文档。

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
  </configSections>
  <log4net>

    <root>
      <level value="ERROR"/>
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ReceiptXmlAppender" />
    </root>

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
      <file value="App_Data/log.txt"/>
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <appender name="ReceiptXmlAppender" type="log4net.Appender.RollingFileAppender" >      
      <param name="File" value="App_Data/ReceiptXml/Receipt.xml"/>
      <param name="RollingStyle" value="Date"/>
      <param name="DatePattern" value="yyyy-MM-dd.\tx\t" />
      <param name="StaticLogFileName" value="false"/>
      <appendToFile value="false" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message %stackTrace%newline" />
      </layout>
    </appender>

  </log4net>
  <appSettings>
    <add key="XmlReceiptPath" value="C:\Development\Payment\trunk\WebsiteFrontEnd\App_Data\ReceiptXml"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

0 个答案:

没有答案