简体   繁体   English

使用外部配置文件时,log4net不记录

[英]log4net not logging when using external config file

I have found other posts with similar problems, but thus far I haven't been able to solve my particular case. 我发现其他帖子有类似的问题,但到目前为止我还没能解决我的具体情况。

If I put the log4net configuration in the app.config then all works fine, but I want it in a separate file. 如果我将log4net配置放在app.config中,那么一切正常,但我希望它在一个单独的文件中。

I have looked at the following links, and have tried to implement some of the proposed solutions (but perhaps I didn't understand it correctly): 我查看了以下链接,并试图实现一些建议的解决方案(但也许我没有正确理解):

Similar SO question, but with web scenario 类似的问题,但与网络情景

Apache documentation, 'Reading Files Directly' section Apache文档,“直接读取文件”部分

Tim Corey's log4net tutorial Tim Corey的log4net教程

So here is my Log4NetSettings.config file's content (wrapped in a configuration element): 这是我的Log4NetSettings.config文件的内容(包含在配置元素中):

<log4net>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="" />
  <commandText value="INSERT INTO [Calculation Engine Log] ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
  <parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout" />
  </parameter>
  <parameter>
    <parameterName value="@thread" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%thread" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@log_level" />
    <dbType value="String" />
    <size value="50" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@logger" />
    <dbType value="String" />
    <size value="255" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%logger" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@message" />
    <dbType value="String" />
    <size value="4000" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>
</appender>


<root>
  <level value="INFO"/>
  <appender-ref ref="AdoNetAppender"/>
</root>
</log4net>

I change the connectionstring value during runtime. 我在运行时更改了connectionstring值。

Here is what I have in the app.config: 这是我在app.config中的内容:

<configSections>
 <section name="log4net"  type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<appSettings>
 <add key="log4net.Config" value="Log4NetSettings.config" />
</appSettings>
<log4net configSource="Log4NetSettings.config" />

In the executable class I have this: 在可执行类中我有这个:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4NetSettings.config", Watch = true)]

and then as soon as I can get hold of the connectionstring and configure it to the AdoNetAppender, I call: 然后,只要我能抓住连接字符串并将其配置到AdoNetAppender,我就会调用:

 log.Info("Testing");

I also have: 我也有:

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

in the class with the assembly attribute and first logging call. 在具有assembly属性和第一次日志记录调用的类中。

The Log4NetSettings.config file has its Build Action set to Content and its 'Copy to Output Directory' set to 'Copy Always'. Log4NetSettings.config文件的Build Action设置为Content,其“Copy to Output Directory”设置为“Copy Always”。

I have been pulling my hair out, and I don't know what I am doing wrong. 我一直在拔头发,我不知道自己做错了什么。 Should I have Log4NetSettings.config as just a normal xml file? 我应该将Log4NetSettings.config作为普通的xml文件吗? Should I put the assembly attribute in the AssemblyInfo.cs file? 我应该将assembly属性放在AssemblyInfo.cs文件中吗? Should I rather call: 我应该打电话:

XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "Log4NetSettings.config"));

than have the assembly attribute? 比具有汇编属性?

It feels like I've tried so many different things, that perhaps I have mixed them up and gotten the wrong combination. 感觉就像我尝试了很多不同的东西,也许我把它们混合起来并得到了错误的组合。

debug = "true"添加到<log4net>标记(即<log4net debug="true"> )会提供许多有用的信息,这些信息可能有助于解决。

Yes, you should have the log4net config in an XML file. 是的,您应该在XML文件中使用log4net配置。 When you have log4net in a separate file, it's no longer related to the app.config so the configuration section is not required, and the config should not be wrapped in a configuration element. 当您将log4net放在单独的文件中时,它不再与app.config相关,因此不需要配置部分,并且配置不应包含在配置元素中。 (It isn't necessary to have the assembly attribute in the AssemblyInfo.cs, and I note you have initialised the logging system in your startup code, which is required when using the assembly attribute technique ) (没有必要在AssemblyInfo.cs中具有assembly属性,并且我注意到你已经在启动代码中初始化了日志系统,这在使用汇编属性技术时必需的

Thus your log4net file will look like this: 因此,您的log4net文件将如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>  
    <!-- config .. -->

If you still have problems, log4net has diagnostic and debugging capability, see this question for how to enable it. 如果仍有问题,log4net具有诊断和调试功能,请参阅此问题以了解如何启用它。

(Also, if you're using log4net 1.2.11 or later, you can set the connection string name in the log4net config , assuming it is in your app.config) (另外,如果您使用的是log4net 1.2.11或更高版本,则可以在log4net配置中设置连接字符串名称 ,假设它位于您的app.config中)

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

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