使用 RollingFileAppender 时出现 Log4Net 问题
我正在使用 log4net 在我的应用程序中执行日志记录。我已将我的项目绑定到 TFS。我已经创建了一个围绕 log4net 的包装器,如下所示:
public static class TestLogger
{
private static readonly ILog log = LogManager.GetLogger("TestLogger");
static TestLogger()
{
log4net.Config.XmlConfigurator.Configure();
}
public static void LogInfo(string information)
{
log.Info(information);
}
public static void LogError(string erroMessage, Exception ex)
{
log.Error(erroMessage, ex);
}
public static void LogWarnings(string warningText)
{
log.Warn(warningText);
}
}
当我尝试从 VS2010 执行该程序时,我发现日志文件没有被创建。我创建另一个项目(未绑定到 TFS)并执行一些日志记录,它成功并在应用程序的 bin/debug 中创建了文件。
下面是我的 log4net 配置文件。
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file value="Log.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="1GB" />
<layout type="log4net.Layout.PatternLayout, log4net">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="TestLogger">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</logger>
</log4net>
有人可以帮助解决这个问题吗?
I am using log4net to perform logging in my application. I have bound my project to TFS. I have created a wrapper around log4net as below:
public static class TestLogger
{
private static readonly ILog log = LogManager.GetLogger("TestLogger");
static TestLogger()
{
log4net.Config.XmlConfigurator.Configure();
}
public static void LogInfo(string information)
{
log.Info(information);
}
public static void LogError(string erroMessage, Exception ex)
{
log.Error(erroMessage, ex);
}
public static void LogWarnings(string warningText)
{
log.Warn(warningText);
}
}
When I tried to execute the program from VS2010 I found that log file is not being created. I create another project (not bound to TFS) and perform some logging, it succeeded and created the file in bin/debug of application.
Below is my log4net configuration file.
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender, log4net">
<file value="Log.txt" />
<appendToFile value="false" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="3" />
<maximumFileSize value="1GB" />
<layout type="log4net.Layout.PatternLayout, log4net">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<logger name="TestLogger">
<level value="ALL" />
<appender-ref ref="RollingFileAppender" />
</logger>
</log4net>
Can anybody help in this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些故障排除提示:
在配置文件中定义日志文件的绝对路径。
检查代码中的当前工作目录(Environment.CurrentDirectory)。如果您在 VS 调试器下运行,并且未在项目属性的“调试”选项卡中指定工作目录,则它很可能默认为当前 Visual Studio 工作目录。
我认为与 TFS 绑定无关。
Some troubleshooting tips:
define an absolute path to the Log file in your config file.
check the current working directory in your code (Environment.CurrentDirectory). If you're running under the VS debugger, and you haven't specified a working directory in the Debug tab of your project properties, it may well default to the current Visual Studio working directory.
I don't think being bound to TFS is relevant.
只需更改这部分即可
Just change this part
也许您的应用程序已经使用了一些声明性配置,它隐藏在代码。搜索类似这样的东西:
[程序集:log4net.Config.XmlConfigurator(Watch=true)]
否则尝试使用以下命令获取 log4net 存储库
log4net.LogManager.GetRepository()
。它返回 ILoggerRepository 类型的对象。您可以尝试使用此对象将有关当前 log4net 配置的一些信息写入控制台或其他地方。Maybe your application already uses some declarative configuration, which is somewhere burried in the code. Search for something like this:
[assembly: log4net.Config.XmlConfigurator(Watch=true)]
Otherwise try to get hold of the log4net repository with
log4net.LogManager.GetRepository()
. It returns an object of the type ILoggerRepository. You can try to use this object to write some information about the current log4net configuration into the Console or somewhere else.尝试按照此处的说明打开内部调试。这应该告诉您问题是什么。如果内部调试没有输出,那么您可能没有配置 log4net。
Try to turn on internal debugging as explained here. This should tell you what the problem is. If there is no output from internal debugging then you probably did not configure log4net.