使用log4net记录到两个不同的日志文件 [英] Logging to two different log files with log4net

查看:100
本文介绍了使用log4net记录到两个不同的日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将错误消息以及调试消息记录到两个不同的文件中.我正在使用两种不同的 文件添加程序来执行此操作.这是我的app.config文件

I am attempting to log error messages as well as debug messages to two different files. I am using two different file appenders to do this. Here is my app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="DebugFileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="DebugLog.log" />
  <param name="AppendToFile" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%date %level %logger - %message %newline" />
  </layout>
  <filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="DEBUG"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<appender name="ErrorFileAppender" type="log4net.Appender.FileAppender">
  <param name="File" value="ErrorLog.log" />
  <param name="AppendToFile" value="true" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%date %level %logger %type %exception %line %location - %message                  %newline" />
  </layout>
  <filter type="log4net.Filter.LevelMatchFilter">
    <levelToMatch value="ERROR"/>
  </filter>
  <filter type="log4net.Filter.DenyAllFilter" />
</appender>

<root>
  <level value="DEBUG" />
  <appender-ref ref="DebugFileAppender" />
</root>

<logger name="ErrorFileAppender" >
  <level value="ERROR"/>
  <appender-ref ref="ErrorFileAppender"/>
</logger>

调试消息可以正常工作,但当我更改程序以包含异常错误,但未写入错误 到文件.这是我用来捕获异常的代码

The debug messages work fine but when i alter the program to contain an exception error, the error is not written to the file. Here is the code i use to catch the exception

try
{
//some code here
}
catch(Exception e)
{
logger.Error(e);
}

推荐答案

如果您的应用程序是Web应用程序,则需要更改log4Net框架的访问权限,以访问Web应用程序文件夹,例如C:\ inetpub \ YourWebApplication \ logs.

If your apps is a web application, you need to change permission for log4Net framework to access the web application folder, for example C:\inetpub\YourWebApplication\logs.

但是,您是否尝试过logger.Debug方法?如果有效,则可能还有另一个问题.

However, have you try the logger.Debug method? if it works, there could be another issue.

山姆.


这篇关于使用log4net记录到两个不同的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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