Click here to Skip to main content
15,940,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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" />

Here, the following code will create
<file value="C:\\TestProj\\TestProj/Info.txt" />
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
Updated 30-Mar-12 2:55am
Xeshan Ahmed 30-Mar-12 8:20am    
insufficient information
Xeshan Ahmed 30-Mar-12 8:21am    
give some details what exactly you want ?

This works:[^]

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" />

(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));
Share this answer
Can't you guys use google?? Try it
Share this answer
ildarXIV 29-Mar-18 13:40pm    
So Google brought me here to your answer: "use Google".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900