log4net fileappender 未在 Outlook 中创建 log-file.txt ThisAddIn.cs
我看不出这里出了什么问题。我只想使用我的 Outlook AddIn 将 log4net 写入日志文件。我的 app.config 文件中有以下内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
以下是我的启动类 ThisAddIn.cs 中的相关语句(注释显示了我尝试过的变体):
//protected static readonly ILog log = LogManager.GetLogger("application-log");
public static readonly ILog log = LogManager.GetLogger(typeof(ThisAddIn));
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//BasicConfigurator.Configure();
//XMLConfigurator.Configure();
log.Info("Application Start");
log.Warn("This is a warning message.");
log.Debug("This is a debug message");
if (log.IsDebugEnabled)
{
log.Debug("This is another debug message");
}
在我对此的研究中,它应该写到我的 project/bin/Debug 文件夹中有一个名为 log-file.txt 的文件,但我没有看到任何创建的文件。当我使用调试器进入代码时,日志对象的方法似乎可以正常工作。我还尝试了以下对该文件的绝对规范,但同样缺乏结果:
<param name="File" value="c:\\try\\logger\\log-file.txt" />
有人能发现我的错误吗?
I can't see what is wrong here. I just want to get log4net writing to a log file with my Outlook AddIn. I have the following in my app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>
<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="LogFileAppender" />
</root>
</log4net>
</configuration>
Here are the relevant statements in my startup class, ThisAddIn.cs (comments show variations I have tried):
//protected static readonly ILog log = LogManager.GetLogger("application-log");
public static readonly ILog log = LogManager.GetLogger(typeof(ThisAddIn));
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
//BasicConfigurator.Configure();
//XMLConfigurator.Configure();
log.Info("Application Start");
log.Warn("This is a warning message.");
log.Debug("This is a debug message");
if (log.IsDebugEnabled)
{
log.Debug("This is another debug message");
}
In my research of this, it should write to a file called log-file.txt in my project/bin/Debug folder but I see nothing created. When I step into the code with the Debugger the methods of the log object appear to work without complaint. I also tried the following absolute specification for the file with the same lack of results:
<param name="File" value="c:\\try\\logger\\log-file.txt" />
Can someone spot my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Log4Net 不会查看您的 app.config,除非您也告诉他。您在 app.config 中编写的 log4net 配置也可以编写在单独的 xml 中,或者以编程方式编写在代码中。
您需要指示 log4net 从哪里获取他的配置。
请参阅:http://logging.apache.org/log4net/release/manual/configuration.html
在您的情况下,最简单的方法就是添加:
Properties\AssemblyInfo.cs 文件中的任何位置。
执行此操作后:仅将“c:\try\logger\log-file.txt”替换为“log-file.txt”,运行程序后,您应该会在“调试”文件夹中看到。
Log4Net doesn't look in your app.config unless you tell him too. The log4net configuration you wrote in app.config could have also been written in a separate xml, or programatically in code.
You need to instruct log4net from where to take his configuration.
See: http://logging.apache.org/log4net/release/manual/configuration.html
The easiest way to do it in your case is just add:
anywhere in your Properties\AssemblyInfo.cs file.
After you do this: replace "c:\try\logger\log-file.txt" with only "log-file.txt" and after you run the program, you should then see in your Debug folder.
对于 Windows 应用程序,您可以将其添加到您的 Program:Main() 方法中:
For Windows apps, you can add this to your Program:Main() method:
不要使用
尝试使用单条
检查您的文件夹权限,不要忘记使用以下命令初始化 global.asax.cs 上的 log4net
Don´t use
<param name="File" value="c:\try\logger\log-file.txt" />
try use single bars instead
<param name="File" value="c:\try\logger\log-file.txt" />
Check your folder permissions and don´t forget to initialize the log4net on global.asax.cs using
在 global.asax 中的应用程序启动中添加以下内容。
log4net.Config.XmlConfigurator.Configure();
Add the following in application start in global.asax.
log4net.Config.XmlConfigurator.Configure();
就我而言,我使用的是 BufferedRollingFileAppender 但我省略了它的 evaluator 元素:
In my case I was using a
BufferedRollingFileAppender
but I omitted itsevaluator
element: