如何追踪log4net问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/756125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:31:47  来源:igfitidea点击:

How to track down log4net problems

.netlogginglog4net

提问by John M Gant

I use log4net all the time, but one thing I've never figured out is how to tell what's going on on the inside. For example, I've got a console appender and a database appender in my project. I made a few changes to the database and the code, and now the database appender doesn't work anymore. I'll figure out why eventually, but it would help a lot if I could see what's going on inside log4net.

我一直在使用 log4net,但我从未想过的一件事是如何判断内部发生了什么。例如,我的项目中有一个控制台 appender 和一个数据库 appender。我对数据库和代码进行了一些更改,现在数据库附加程序不再工作了。我最终会弄清楚原因,但如果我能看到 log4net 内部发生了什么,那将会有很大帮助。

Does log4net generate any kind of output that I can view to try to determine the source of my problem?

log4net 是否生成任何类型的输出,我可以查看以尝试确定问题的根源?

回答by David Espart

First you have to set this value on the application configuration file:

首先,您必须在应用程序配置文件上设置此值:

<configuration>
   <appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
   </appSettings>
</configuration>

Then, to determine the file in which you want to save the output you can add the following code in the same .config file:

然后,要确定要保存输出的文件,您可以在同一个 .config 文件中添加以下代码:

<configuration>
...

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

...
</configuration>

You can find a more detailed explanation under 'How do I enable log4net internal debugging?' in the log4net FAQ page.

您可以在“如何启用 log4net 内部调试?”下找到更详细的说明。在log4net 常见问题页面中

回答by Bolo

If you are using a log4net config file you can also turn on the debugging there by changing the top node to:

如果您使用的是 log4net 配置文件,您还可以通过将顶部节点更改为:

<log4net debug="true">

This will work once the config is reloaded and assuming your trace listener is setup properly.

一旦重新加载配置并假设您的跟踪侦听器设置正确,这将起作用。

回答by JP Toto

In addition to the above answer, you can use this line to see the log in realtime instead of the c:\tmp\log4net.txt output.

除了上面的答案,您可以使用这一行来实时查看日志,而不是 c:\tmp\log4net.txt 输出。

log4net.Util.LogLog.InternalDebugging = true;

For example, in a console app, you can add this and then watch the output in realtime. It's good for debugging log4net in a small test harness to see what's happening with the appender you're testing.

例如,在控制台应用程序中,您可以添加它,然后实时观察输出。在小型测试工具中调试 log4net 以查看您正在测试的 appender 发生了什么非常有用。

回答by K0D4

Make sure the root application where your entry point is logs something to log4net. Give it one of these:

确保入口点所在的根应用程序将某些内容记录到 log4net。给它以下之一:

private static ILog logger = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
    logger.InfoFormat("{0} v.{1} started.", Assembly.GetExecutingAssembly().GetName().Name, Assembly.GetExecutingAssembly().GetName().Version.ToString());

With 2.0.8, I had an interesting situation. I created a library project and a test exe project that would demo it's capabilities. The library project was set up to use Log4net as was the exe project. The exe project used the assemblyinfo attribute to register the config, yet I was getting no logging output to either the console or the log file. When I turned on log4net internal debug logging, I got some internal messages written to the console, but still none of my normal logs. No errors were being reported. It all started working when I added the above code to my program. Log4net was otherwise setup correctly.

在 2.0.8 中,我遇到了一个有趣的情况。我创建了一个库项目和一个测试 exe 项目来演示它的功能。库项目被设置为使用 Log4net 作为 exe 项目。exe 项目使用 assemblyinfo 属性来注册配置,但我没有得到控制台或日志文件的日志输出。当我打开 log4net 内部调试日志记录时,我收到了一些写入控制台的内部消息,但仍然没有我的正常日志。没有报告错误。当我将上面的代码添加到我的程序时,一切都开始工作了。Log4net 以其他方式正确设置。

回答by olaf870

In log4net 2.0.8 it seems not to be possible to make the logging with log4net in a separate DLL. If I tried this, the results are very strange: No logging is performed anymore. And the initialization of log4net with the debug option shows no Errors.

在 log4net 2.0.8 中,似乎不可能在单独的 DLL 中使用 log4net 进行日志记录。如果我尝试这样做,结果会很奇怪:不再执行日志记录。并且带有调试选项的 log4net 初始化显示没有错误。

Like K0D4 said, you should have a reference to log4net in your main-module and should it call once on the start of the Programm and all is fine.

就像 K0D4 所说的,你应该在你的主模块中有一个对 log4net 的引用,并且它应该在程序开始时调用一次,一切都很好。

In the next version of log4net, this bug will be probably be fixed.

在 log4net 的下一个版本中,这个错误可能会被修复。

回答by StuartN

If the internal log doesn't give you enough information, it's very easy to build and debug the source code. If you don't want to mix this up with your development project, add a simple console application which just logs a message, copy your project's log4net.configto this application, and debug the class in question.

如果内部日志没有为您提供足够的信息,则构建和调试源代码非常容易。如果您不想将其与您的开发项目混淆,请添加一个简单的控制台应用程序,该应用程序只记录一条消息,将您的项目复制log4net.config到该应用程序,然后调试相关类。