简体   繁体   English

将 log4net 文件路径设置为自定义路径

[英]Set log4net file path to a custom path

I need to change the log file path to a custom path.我需要将日志文件路径更改为自定义路径。

I have a code where it makes the file in the wwwroot folder.我有一个代码,它可以在 wwwroot 文件夹中生成文件。

  public static ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

    protected void Application_Start(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure();

        FileAppender rootAppender = (FileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root.Appenders[0];           
        string filename = rootAppender.File;
        Global.logger.Error("Log 4 net filename: " + filename);         
    }

please can some1 help me with this.请有人帮我解决这个问题。

Thanks谢谢

This is what is there in my config file.这是我的配置文件中的内容。

   <!-- Log file locaation -->
<param name='File' value='TS_GUI_Logs.coplog'/>
<param name='AppendToFile' value='true'/>

I do not want to set the File name in the config.我不想在配置中设置文件名。 Instead i want to set it from my code behind.相反,我想从我后面的代码中设置它。

Have you checked your config file?你检查过你的配置文件了吗? This code reads the file name from the config file.此代码从配置文件中读取文件名。

If you need a custom path for everything, change the path in the config file.如果您需要所有内容的自定义路径,请更改配置文件中的路径。 (Just make sure you you know who all the consumers of this setting are in your code.) If you need a custom path for one thing, you will need to create a new FileAppender. (只要确保您知道您的代码中此设置的所有使用者是谁。)如果您需要一个事物的自定义路径,您将需要创建一个新的 FileAppender。

You can have log4net config like this:你可以有这样的 log4net 配置:

<log4net debug="true">
  <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
    <file type="log4net.Util.PatternString" value="%property{LogFileName}" />

and the application start code will like this:应用程序启动代码将如下所示:

    log4net.GlobalContext.Properties["LogFileName"] = "C:\\aaaaa.log";
    log4net.Config.XmlConfigurator.Configure();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM