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:
Hello,

How we can save the log4net log file to a relative path.

The code I have is as follows...
C#
<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" />
  </layout>
</appender>


Here, the following code will create
C#
<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
Posted
Updated 30-Mar-12 2:55am
v2
Comments
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: http://geekswithblogs.net/wpeck/archive/2009/10/08/setting-log4net-fileappender.file-at-runtime.aspx[^]

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.
XML
<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:
XML
<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):
C#
fileAppender.File = Path.Combine(logDirectory, Path.GetFileName(fileAppender.File));
 
Share this answer
 
v4
Can't you guys use google?? Try it
 
Share this answer
 
Comments
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