log4net的输出到我的文档 [英] Log4net output to My Documents

查看:240
本文介绍了log4net的输出到我的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的C#应用​​程序,我们将文件写入 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)。我们log4net的日志文件应该去那里了,所以我们定义 application.conf 如下:

In our C# app, we write files to Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments). Our log4net logfile should go there too, so we've defined application.conf as follows:

<appender name="LogFile" type="log4net.Appender.RollingFileAppender">
  <appendToFile value="true"/>
  <file value="%USERPROFILE%\My Documents\MyApp\log.txt"/>
  ...snip...
</appender>

这工作,直到我们有一个非英语Windows的PC上运行英寸因为那样的话, SpecialFolder.MyDocuments 指向文件夹 Mijn Documenten ,而日志还是到我的文档。随之而来的混乱,因为现在我们的文件是在两个地方。

This works, until we run in on a PC which has a non-English Windows. Because then, SpecialFolder.MyDocuments points to the folder Mijn Documenten, while the log still goes to My Documents. Confusion ensues, because now our files are in two places.

我想我的日志写入真正的我的文档文件夹中。我该怎么做呢?

I want to write my log to the "real" My Documents folder. How do I do this?


  • 我试图找到像%USERPROFILE%,但似乎并不存在一个我的文档。

  • 有一个定义的我的文档的真实位置的注册表项,但它从 application.conf

  • 我试图以编程方式重写我的appender的文件参数,像这样的:

  • I tried to find an environment variable like %USERPROFILE%, but there doesn't seem to exist one for My Documents.
  • There's a registry key that defines the true location of My Documents but it's not accessible from application.conf.
  • I tried to override the File parameter of my appender programmatically, like this:

public static void ConfigureLogger()
{
    XmlConfigurator.Configure();

    Hierarchy hierarchy = (Hierarchy)log4net.LogManager.GetRepository();
    foreach (var appender in hierarchy.Root.Appenders)
    {
        if (appender is FileAppender)
        {
            var fileAppender = appender as FileAppender;
            var logDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "MyApp");
            var pathElements = fileAppender.File.Split(Path.DirectorySeparatorChar);
            var logFileName = pathElements.Last();
            var fullLogFilePath = Path.Combine(logDirectory, logFileName);
            fileAppender.File = fullLogFilePath;
        }
    }
}

这也不起作用:当我检查我的记录器的内部,在文件属性高兴地报告 Mijn Documenten ,但在平均时间日志还是去我的文档

This doesn't work either: when I inspect the internals of my logger, the File property happily reports Mijn Documenten, but in the mean time the logs still go to My Documents.

我运行的想法!

推荐答案

更改行

<file value="%USERPROFILE%\My Documents\MyApp\log.txt"/>



to

<file type="log4net.Util.PatternString" value="%envFolderPath{MyDocuments}\MyApp\log.txt" />

这篇关于log4net的输出到我的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆