我如何编程方式更改文件位置? [英] How can I change the file location programmatically?

查看:194
本文介绍了我如何编程方式更改文件位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全新log4net的。结果
我设法得到的东西通过增加一个配置文件和简单的日志记录功能。结果
我已经很难codeD的值为C:\\ TEMP \\ log.txt的但是这还不够好。

I am totally new to Log4net.
I have managed to get something going by adding a config file and simple logging.
I have hardcoded the value to be "C:\temp\log.txt" but this is not good enough.

日志必须到特殊文件夹

path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

和这个路径的变化取决于您是否正在使用Windows Server 2008或Windows XP或Vista等...

and this path changes depending whether you are using Windows Server 2008 or Windows XP or Vista etc...

如何才能更改文件的位置log4net的编程?

How can I just change the location of the file in log4net programmatically?

这是我做了什么:

<configSections>
<section name="log4net"
         type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net"/>
</configSections>
<log4net>         
    <root>
        <level value="DEBUG" />
        <appender-ref ref="LogFileAppender" />
    </root>
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
        <param name="File" value="C:\temp\log.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>
</log4net>


class Program
{
    protected static readonly ILog log = LogManager.GetLogger(typeof(Program));

    static void Main(string[] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        log.Warn("Log something");

        path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);


        // How can I change where I log stuff?
    }
}

只是需要弄清楚如何更改登录的东西,我想。

Just need to figure out how I can change to log stuff to where I want to.

有什么建议?
非常感谢

Any suggestions? Thanks a lot

推荐答案

log4net的能为你处理这个问题。字符串类型的任何附加器属性可以被格式化,在这种情况下,使用<一href=\"http://logging.apache.org/log4net/release/sdk/log4net.Util.PatternString.html\">log4net.Util.PatternString选择处理程序。 PatternString甚至支持SpecialFolder枚举,可实现以下配置优雅:

log4net can handle this for you. Any appender property of type string can be formatted, in this case, using the log4net.Util.PatternString option handler. PatternString even supports the SpecialFolder enum which enables the following elegant config:

<appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >
    <file type="log4net.Util.PatternString" 
        value="%envFolderPath{CommonApplicationData}\\test.txt" />
    ...
</appender>

下面是样张布丁一个单元测试:

Here's a unit test that proofs the pudding:

[Test]
public void Load()
{
    XmlConfigurator.Configure();
    var fileAppender = LogManager.GetRepository()
        .GetAppenders().First(appender => appender is RollingFileAppender);

    var expectedFile = 
        Path.Combine(
            Environment.GetFolderPath(
                Environment.SpecialFolder.CommonApplicationData),
                "test.txt");

    Assert.That(fileAppender, 
        Is.Not.Null & Has.Property("File").EqualTo(expectedFile));
}

下面的测试验证log4net的实际写入到磁盘(这基本上使他们成为一体化的测试,而不是一个单元测试,但我们会留在它):

The following test verifies that log4net actually writes to disk (which basically makes this an "integration" test, not a unit test, but we'll leave it at that for now):

[Test]
public void Log4net_WritesToDisk()
{
    var expectedFile = 
        Path.Combine(
            Environment.GetFolderPath(
                Environment.SpecialFolder.CommonApplicationData),
                "test.txt");

    if (File.Exists(expectedFile))
        File.Delete(expectedFile);

    XmlConfigurator.Configure();

    var log = LogManager.GetLogger(typeof (ConfigTest));
    log.Info("Message from test");

    LogManager.Shutdown();

    Assert.That(File.ReadAllText(expectedFile), 
        Text.Contains("Message from test"));
}

注:我强烈建议使用上述示例中展示了紧凑的属性语法。删除所有这些&LT;属性名=让你的配置是更具有可读性。

NB: I strongly suggest using the compact property syntax demonstrated in the above sample. Removing all those "<property name=" makes your config that much more readable.

这篇关于我如何编程方式更改文件位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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