如何包括log4net的一类库? [英] How to include log4net for a class library?

查看:121
本文介绍了如何包括log4net的一类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现日志记录功能,成为一个类库,它本身就是一个web服务引用。我尝试添加的app.config和做的一切都是需要的,但它似乎是,当一个异常被抛出,log4net的根本不起任何作用。

I want to implement logging function into a class library, which is itself referenced in a webservice. I tried to add app.config and did everything needed, but it seems that when an exception is thrown, log4net simply does nothing.

我的app.config

my app.config

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="D:\\mylogfile.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="test" />
      </filter>
      <filter type="log4net.Filter.StringMatchFilter">
        <stringToMatch value="error" />
      </filter>
      <filter type="log4net.Filter.DenyAllFilter" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %level %logger - %message%newline%exception" />
      </layout>
    </appender>
    <root>
      <level value="INFO"/>
      <appender-ref ref="RollingFileAppender"/>
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>

在AssemblyInfo.cs中:

in AssemblyInfo.cs:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "app.config")]

在LogManager.cs:

in LogManager.cs:

private static readonly ILog Log = log4net.LogManager.GetLogger
            (MethodBase.GetCurrentMethod().DeclaringType);
public static void WriteLog(Exception ex)
{
    Log.Error(ex);
}

能否请你告诉我,什么是错的?我怎样才能log4net的工作为我的类库?

Can you please tell me what's wrong? How can I get log4net working for my class library?

感谢您

推荐答案

在运行时,配置文件总是从主机应用程序中使用,除非明确声明。在此情况下,web.config文件正在使用无法app.cofig。而提到一些其他的自定义配置文件名,并确保文件被复制到Web服务的虚拟目录。另外,作为chibacity说,确保日志文件的权限。最好把它放在App_Data文件夹中的Web服务主机。

At runtime, config file is always used from host application, unless declared explicitly. Here in case, web.config is being used not app.cofig. Rather mention some other custom config file name and ensure that file is copied in virtual directory of web service. Also as chibacity said, ensure permission on log file. Better keep it in app_data folder for web service host.

这篇关于如何包括log4net的一类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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