简体   繁体   English

启用了log4net,但未创建任何文件

[英]log4net enabled, but not creating any files

I've been shown how to use log4net and got a config file to use. 我已经展示了如何使用log4net并获得了要使用的配置文件。 I lost this config file and now I cant get it to work. 我丢失了此配置文件,但现在无法正常工作。 For some reason the guy who configured it could not get it to work while having the configuration info in app.config so he placed it in log4net.config in the output folder. 由于某种原因,配置它的人在app.config中拥有配置信息时无法使它工作,因此他将其放置在输出文件夹中的log4net.config中。

He then called this when the program started up: 然后,在程序启动时,他调用了此方法:

        var logConfig = Path.Combine(Path.GetDirectoryName(typeof(App).Assembly.Location), "log4net.config");
        var configStream = new FileInfo(logConfig);
          log4net.Config.XmlConfigurator.Configure(configStream);

And it worked flawlessly. 它完美地工作了。

Now I got it to work perfectly in a new console project. 现在,我在一个新的控制台项目中使其完美工作。 However allmost all of my projects are addons to another application so my project types are class libraries loaded by this other application (called Revit). 但是,我所有的所有项目都是所有其他应用程序的插件,因此我的项目类型是该其他应用程序(称为Revit)加载的类库。

Anyway, so I know that I can log, but just cant get it to work... This is log4net.config that I use: 无论如何,所以我知道我可以登录,但无法运行...这是我使用的log4net.config:

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
    <file value="logs\" />
    <datePattern value="dd.MM.yyyy'.log'" />
    <staticLogFileName value="false" />
    <appendToFile value="true" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="10" />
    <maximumFileSize value="5MB" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
    </layout>
  </appender>
  <root>
    <level value="ALL" />
    <appender-ref ref="RollingFileAppender"/>
  </root>
</log4net>

Each class that I want to log stuff from declares the an ILog instance on the class level. 我要从中记录内容的每个类都在类级别上声明一个ILog实例。 Like this: 像这样:

static private ILog _logger = LogManager.GetLogger(typeof(Program));

The ILog instance reports that all logging is enabled (debug and so on), but there are no files created. ILog实例报告已启用所有日志记录(调试等),但未创建任何文件。

Do I need to do anything else? 我还需要做其他事情吗?

Im going nuts here! 我在这里疯了! Thanks for any help! 谢谢你的帮助!

It seems you tried a few different ways to configure log4net. 似乎您尝试了几种不同的方法来配置log4net。 If one fails, the best option is to enable log4net debugging to see if your logger is working/crashed. 如果失败,最好的选择是启用log4net调试,以查看记录器是否正在工作/崩溃。 In your app.config: 在您的app.config中:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Log4net FAQ Log4net常见问题解答

In App.config file you should add this code in order to register the log4net section handler and the log will be generated 在App.config文件中,您应该添加此代码以注册log4net节处理程序,并且将生成日志

<configuration>
  <configSections>
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  ......
</configuration>

or Check if your AssemblyInfo.cs file has this code : 或检查您的AssemblyInfo.cs文件是否具有以下代码:

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

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

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