简体   繁体   English

如何为类库包含log4net?

[英]How to include log4net for a class library?

I want to implement logging function into a class library, which is itself referenced in a webservice. 我想将日志功能实现到类库中,类库本身在web服务中引用。 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并完成所需的一切,但似乎抛出异常时,log4net什么都不做。

my app.config 我的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>

in AssemblyInfo.cs: 在AssemblyInfo.cs中:

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

in LogManager.cs: 在LogManager.cs中:

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

Can you please tell me what's wrong? 你能告诉我什么是错的吗? How can I get log4net working for my class library? 如何让log4net为我的类库工作?

Thank you 谢谢

At runtime, config file is always used from host application, unless declared explicitly. 在运行时,除非明确声明,否则始终从主机应用程序使用配置文件。 Here in case, web.config is being used not app.cofig. 在这种情况下,web.config正在使用而不是app.cofig。 Rather mention some other custom config file name and ensure that file is copied in virtual directory of web service. 而是提及一些其他自定义配置文件名,并确保将文件复制到Web服务的虚拟目录中。 Also as chibacity said, ensure permission on log file. 同样如chibacity所说,确保对日志文件的权限。 Better keep it in app_data folder for web service host. 最好将它保存在web服务主机的app_data文件夹中。

You could use some kind of injection, either you build a simple one like: 您可以使用某种注射方式,或者构建一个简单的注射方式,例如:

public interface ILogger
{
    void LogInfo(string message);
       .
       .
       .
}

And then you just inject something that matches that interface, like a wrapper for log4net or such. 然后你只需要注入与该接口匹配的东西,比如log4net等的包装器。

But, I think the most correct thing is to not log in the class library. 但是,我认为最正确的是不登录类库。 Why I think so is because the library itself is not the application, your web service is so your web service should decide what to log. 为什么我这么认为是因为库本身不是应用程序,您的Web服务是这样的,因此您的Web服务应该决定记录什么。 If you want to log exceptions just don't catch them in the class library and let your web service handle the logging. 如果要记录异常,请不要在类库中捕获它们,并让您的Web服务处理日志记录。 This will also make it easier to centralize the logging. 这也可以更容易集中日志记录。

Please see my answer to the following question: 请参阅我对以下问题的回答:

Use log4net in SDK 在SDK中使用log4net

It has a log configuration routine that will construct a complete path to a log4net config file. 它有一个日志配置例程,它将构建一个log4net配置文件的完整路径。 As you are in a webservice it my not be looking where you expect for "app.config". 当你在网络服务中时,我不会看到你对“app.config”的期望。

Also make sure that you have permission to write to "D:\\mylogfile.txt" from your webservice. 还要确保您有权从Web服务写入“D:\\ mylogfile.txt”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM