相对路径中的Log4net日志文件 [英] Log4net log file in relative path
问题描述
你好,
我们如何将log4net日志文件保存到相对路径.
我的代码如下...
Hello,
How we can save the log4net log file to a relative path.
The code I have is as follows...
<appender type="log4net.Appender.RollingFileAppender" name="DebugLog">
<file value="C:\\TestProj\\TestProj/Info.txt" />
<layout type="log4net.Layout.PatternLayout">
<conversionpattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
在这里,以下代码将创建
Here, the following code will create
<file value="C:\\TestProj\\TestProj/Info.txt" />
C:\ TestProj 中的日志文件,但是如您所见,该路径是绝对路径,我想指定相对路径;
任何帮助都很好.
the log file in C:\TestProj, but as you see, the path is absolute, I want to specify the relative path;
any help would be great
推荐答案
这有效: ^ ]
确保在调用 log4net.Config.XmlConfigurator.Configure()之后调用上述 Initialize 方法.本文未提及,但可以忽略.
在运行时,配置文件中定义的任何路径都将被 logDirectory 替换,而日志文件的名称保持不变.
This works: http://geekswithblogs.net/wpeck/archive/2009/10/08/setting-log4net-fileappender.file-at-runtime.aspx[^]
Be sure that the mentioned Initialize method is called after log4net.Config.XmlConfigurator.Configure() is called. This is not mentioned in the article but can be overlooked.
At runtime any path defined in the config file will be replaced by logDirectory while the name of the log file remains the same.
<param name="File" value="C:\\TestProj\\TestProj/Info.txt" />
(检查/Info.txt中的正斜杠是否可以使用)
也许您不想替换完整的路径,而是在运行时添加基本目录.配置文件如下所示:
(check if the forward slash in /Info.txt is OK to use)
Maybe you do not want to replace the complete path but add a base directory at runtime instead. The config file would look like this:
<param name="File" value="\\Some\\Relative\\Path\\Info.txt" />
然后需要更改以下代码行,以连接基本路径和相对路径(我想您可以自己弄清楚这一点):
Then the following code line needs to change in order to concatenate the base and relative path (I guess you can figure that out for yourself):
fileAppender.File = Path.Combine(logDirectory, Path.GetFileName(fileAppender.File));
你们不能使用谷歌?? 尝试
Can''t you guys use google?? Try it
这篇关于相对路径中的Log4net日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!