0

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.

  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.

user175084
  • 4,550
  • 28
  • 114
  • 169

2 Answers2

1

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.

Les
  • 3,150
  • 4
  • 29
  • 41
  • I want to set the path from the code behind. I have a code to get the path and i want to set that path for the log file. – user175084 Jul 23 '12 at 22:01
  • 1
    See this question then: http://stackoverflow.com/questions/571876/best-way-to-dynamically-set-an-appender-file-path?rq=1 – Les Jul 23 '12 at 22:26
0

You can have log4net config like this:

<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();
Raghav
  • 8,772
  • 6
  • 82
  • 106