使用外部配置文件时,log4net无法记录日志 [英] log4net not logging when using external config file

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

问题描述

我发现了其他类似问题的帖子,但是到目前为止,我还无法解决我的特殊情况.

I have found other posts with similar problems, but thus far I haven't been able to solve my particular case.

如果我将log4net配置放在app.config中,则一切正常,但是我希望将其保存在单独的文件中.

If I put the log4net configuration in the app.config then all works fine, but I want it in a separate file.

我查看了以下链接,并尝试实现一些建议的解决方案(但也许我没有正确理解):

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):

类似的问题,但适用于Web场景

Apache文档,直接读取文件"部分

Tim Corey的log4net教程

这是我的Log4NetSettings.config文件的内容(包装在配置元素中):

So here is my Log4NetSettings.config file's content (wrapped in a configuration element):

<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.

这是我在app.config中拥有的:

Here is what I have in the 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)]

,然后只要我掌握了连接字符串并将其配置为AdoNetAppender,我就会致电:

and then as soon as I can get hold of the connectionstring and configure it to the AdoNetAppender, I call:

 log.Info("Testing");

我也有:

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

在具有Assembly属性和第一次日志记录调用的类中.

in the class with the assembly attribute and first logging call.

Log4NetSettings.config文件的构建操作"设置为内容",复制到输出目录"设置为始终复制".

The Log4NetSettings.config file has its Build Action set to Content and its 'Copy to Output Directory' set to 'Copy Always'.

我一直在拔头发,我不知道自己在做什么错. 我应该将Log4NetSettings.config当作普通的xml文件吗? 我应该将Assembly属性放在AssemblyInfo.cs文件中吗? 我应该打电话给吗?

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? Should I put the assembly attribute in the AssemblyInfo.cs file? Should I rather call:

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

有没有Assembly属性?

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.

推荐答案

是的,您应该在XML文件中包含log4net配置.当log4net位于单独的文件中时,它不再与app.config相关,因此不需要配置部分,并且不应将config包装在配置元素中. (没有必要在AssemblyInfo.cs中具有assembly属性,并且我注意到您已经在启动代码(

Yes, you should have the log4net config in an XML file. 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. (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)

因此,您的log4net文件将如下所示:

Thus your log4net file will look like this:

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

如果仍然有问题,则log4net具有诊断和调试功能,请参阅此问题启用它.

If you still have problems, log4net has diagnostic and debugging capability, see this question for how to enable it.

(此外,如果您使用的是log4net 1.2.11或更高版本,则可以

(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无法记录日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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