We use log4net 1.2 beta 8 in our web applications for tracing and debugging purposes. Log4Net is great, but as with any community project, you always wish it had built in functionality to do a few extra things. In our case, I wanted to be able to log all statements to a file in a folder that exists off the web root. By default, log4net will log to a file relative to the virtual directory where the application that is logging is defined. For example, the following log4net configuration will log to a file named TmsLog.txt in the same folder as the web.config that this entry exists in:
156 <log4net>
157 <appender name="TMSAppender" type="log4net.Appender.FileAppender,log4net">
158 <param name="File" value="TmsLog.txt" />
159 <param name="AppendToFile" value="true" />
160 <layout type="log4net.Layout.PatternLayout,log4net">
161 <param name="ConversionPattern" value="%d %-5p - %m%n" />
162 </layout>
163 </appender>
164 <root>
165 <priority value="DEBUG" />
166 <appender-ref ref="TMSAppender" />
167 </root>
168 </log4net>
I wanted to be able to specify the file location at run time, which lead me into a few traps.
A coworker pointed out that I could simply use relative paths in the configuration file:
158 <param name="File" value="..\\LogLocation\\TmsLog.txt" />
This is ok, but our Dev, QA, and Production environments are all a little different. We'd written a utility object that uses some logic to determine the current execution environment and return the correct folder to use for logging purposes. What I really wanted was to be able to use the information provided by this library to change the logging folder at runtime. One option was to create the configuration programmatically, but I wanted to stay with the basic DomConfigurator.Configure() solution.
The first programmatic solution that I tried was to change the file path at runtime. The following code does allow me to change the path of the configured path, but for some reason it didn't 'stick', and the path used in the configuration file was used instead:
22 ILog myLog=LogManager.GetLogger(typeof(Logger));
23 ILoggerRepository aRepos = myLog.Logger.Repository;
24 foreach(IAppender anAppender in ((log4net.Repository.Hierarchy.Hierarchy)aRepos).Root.Appenders)
25 if (anAppender is FileAppender)
26 ((FileAppender)anAppender).File = @"c:\temp\test.txt";
27 return myLog;
My shorthand approach to this was the following:
28 public static string LogFile
29 { 30 get{ return ((FileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).Root.Appenders[0]).File;} 31 set{ ((FileAppender)((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetLoggerRepository()).Root.Appenders[0]).File = value;} 32 }
Again, no dice. Finally, after a little digging, I came across an email on the log4Net discussion list that pointed me in the right direction. I created my own FileAppender class that derived from the log4net FileAppender, and overwrote the File property:
76 public class InsarioWebLogFileAppender : log4net.Appender.FileAppender
77 { 78 public override string File
79 { 80 get{return base.File;} 81 set{base.File = Web.WebEnvironment.LogDirectory + value;} 82 }
83 }
Originally I tried to store the file setting in a local variable and return it in the getter but that didn't work, probably due to caching and optimization techniques used in the internals of log4net. Instead, I had to set the property on the base entity, as you see above.
posted on Tuesday, November 30, 2004 9:49 AM
Feedback
# re: Setting the log file location at runtime with a DOM configured log4net
1/25/2005 8:20 AM |
This postback is good for one beer.
Merely contact me at the email address described below and then if we're in the same city some time, I'll buy you a beer. I'm in Portland, OR.
I needed exactly what you posted, and your code worked perfectly for me. Thanks very much.
Email name is six letters: pdx (our airport) jjb (my initials) at hotmail. Hope to hear from you.
# re: Setting the log file location at runtime with a DOM configured log4net
4/5/2006 3:09 AM |
Hi,
can you send me some sample code snippet for implementing the same to juliusseizeher@gmail.com. Thanks
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2007 10:41 AM |
I have the same situation with environments. Thanks a lot!!
# re: Setting the log file location at runtime with a DOM configured log4net
12/3/2007 9:30 AM |
This worked for me:
public static void UpdateFileAppenderPaths(string theNewPath) {
log4net.Repository.Hierarchy.Hierarchy h =
(log4net.Repository.Hierarchy.Hierarchy) log4net.LogManager.GetRepository();
foreach (IAppender a in h.Root.Appenders) {
if (a is FileAppender) {
FileAppender fa = (FileAppender) a;
fa.File = theNewPath;
fa.ActivateOptions();
}
}
}
# re: Setting the log file location at runtime with a DOM configured log4net
1/20/2009 6:22 AM |
Hello:)
can you send some sample code snippet for implementing the same to this topic or my email tselofan1@yandex.ru. Thanks..
# Bag of Links #1 » Chinh Do
1/23/2009 5:54 PM |
Bag of Links #1 » Chinh Do
# re: Setting the log file location at runtime with a DOM configured log4net
2/6/2009 1:19 PM |
Nice Article. Thank You.
# re: Setting the log file location at runtime with a DOM configured log4net
2/19/2009 11:29 AM |
Thankss you..
# re: Setting the log file location at runtime with a DOM configured log4net
2/26/2009 3:55 AM |
Nice Article. Thank You.
# re: Setting the log file location at runtime with a DOM configured log4net
3/9/2009 4:01 PM |
thankss u much
# re: Setting the log file location at runtime with a DOM configured log4net
3/9/2009 4:02 PM |
I put it on youtube
# re: Setting the log file location at runtime with a DOM configured log4net
3/10/2009 4:30 PM |
thx
# re: Setting the log file location at runtime with a DOM configured log4net
3/18/2009 10:58 AM |
thanks best regards
# re: Setting the log file location at runtime with a DOM configured log4net
3/18/2009 11:00 AM |
vrey very good yavrum
# re: Setting the log file location at runtime with a DOM configured log4net
3/18/2009 11:01 AM |
thankss u muchs
# re: Setting the log file location at runtime with a DOM configured log4net
3/18/2009 11:02 AM |
Article. Thank You baby
# re: Setting the log file location at runtime with a DOM configured log4net
3/18/2009 11:04 AM |
situation with environments. Thanks a lot!!
# re: Setting the log file location at runtime with a DOM configured log4net
3/19/2009 4:18 PM |
Thx.
# re: Setting the log file location at runtime with a DOM configured log4net
3/26/2009 12:33 AM |
thanks best regards
# re: Setting the log file location at runtime with a DOM configured log4net
4/5/2009 11:23 AM |
thnx
# re: Setting the log file location at runtime with a DOM configured log4net
4/7/2009 5:41 PM |
Thanks.
4/11/2009 12:46 AM |
I read your article.The things you have written sound very sincere and nice topics i am looking forward to its continuation.
4/11/2009 12:48 AM |
I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings. I congratulate you.
4/11/2009 12:48 AM |
I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings. I congratulate you.
4/11/2009 12:49 AM |
The informations are so lovely and so usefull so thank you very much. Be sure i will use all of them keeping in my mind.Have a goog luck.
# re: Setting the log file location at runtime with a DOM configured log4net
4/11/2009 1:49 AM |
thanks I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings. I congratulate you.
# re: Setting the log file location at runtime with a DOM configured log4net
4/16/2009 1:49 PM |
Thanks alot
4/20/2009 1:16 PM |
can you send some sample code snippet for implementing the same to this topic
# re: Setting the log file location at runtime with a DOM configured log4net
4/22/2009 12:25 PM |
Thanks admin
# re: Setting the log file location at runtime with a DOM configured log4net
4/23/2009 12:46 PM |
thanks I like very much the writings and pictures and explanations in your adress so I look forward to see your next writings. I congratulate you.
# re: Setting the log file location at runtime with a DOM configured log4net
4/23/2009 3:29 PM |
nice Article. thanks
# re: Setting the log file location at runtime with a DOM configured log4net
4/24/2009 5:25 AM |
thanks very nice code
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:18 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:19 AM |
thanks you
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:20 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:21 AM |
thanks you
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:22 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
4/27/2009 1:23 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
5/4/2009 2:45 AM |
Hello:)
can you send some sample code snippet for implementing the same to this topic or my email kamlesh.shewani@sgcib.com. Thanks..
# re: Setting the log file location at runtime with a DOM configured log4net
5/8/2009 5:32 AM |
Thanks a lot.
5/21/2009 9:39 AM |
yes good like them it. nice post and nice subject. good luck
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:06 PM |
thanksss alll very nice
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:07 PM |
very very nice....
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:08 PM |
thankss alll for ur time
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:09 PM |
herkese cok tesekkurler
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:11 PM |
airbag tamir servisi
# re: Setting the log file location at runtime with a DOM configured log4net
5/23/2009 6:13 PM |
domain tescil
# re: Setting the log file location at runtime with a DOM configured log4net
5/27/2009 7:25 AM |
thanks so much
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:51 PM |
thanks very much
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:52 PM |
i love you
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:54 PM |
very good
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:55 PM |
hmmmmi
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:57 PM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
5/30/2009 5:58 PM |
free radio
# re: Setting the log file location at runtime with a DOM configured log4net
5/31/2009 4:35 AM |
thank you..
# re: Setting the log file location at runtime with a DOM configured log4net
5/31/2009 11:46 PM |
Thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/2/2009 4:33 PM |
Thank you
# re: Setting the log file location at runtime with a DOM configured log4net
6/2/2009 4:36 PM |
Nice work thanx
# re: Setting the log file location at runtime with a DOM configured log4net
6/3/2009 6:40 AM |
thanks you well
# re: Setting the log file location at runtime with a DOM configured log4net
6/3/2009 6:41 AM |
thanks for you nice
# re: Setting the log file location at runtime with a DOM configured log4net
6/3/2009 6:42 AM |
thanks admin
# re: Setting the log file location at runtime with a DOM configured log4net
6/3/2009 6:43 AM |
merci admin
# re: Setting the log file location at runtime with a DOM configured log4net
6/3/2009 6:44 AM |
Thank you post
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:12 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:13 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:15 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:16 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:17 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:18 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/4/2009 3:19 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/6/2009 9:51 AM |
thank you
# re: Setting the log file location at runtime with a DOM configured log4net
6/6/2009 11:36 PM |
Thanks
6/9/2009 4:24 AM |
thanks you very muck
# re: Setting the log file location at runtime with a DOM configured log4net
6/9/2009 9:56 AM |
thankss
# re: Setting the log file location at runtime with a DOM configured log4net
6/12/2009 1:35 PM |
thansskk
# re: Setting the log file location at runtime with a DOM configured log4net
6/12/2009 1:36 PM |
tank youuuu
# re: Setting the log file location at runtime with a DOM configured log4net
6/15/2009 12:58 AM |
Perfect your site thanks.
# re: Setting the log file location at runtime with a DOM configured log4net
6/15/2009 12:59 AM |
Perfect your site thanks.
# re: Setting the log file location at runtime with a DOM configured log4net
6/15/2009 1:01 AM |
Perfect your site thanks.....
# re: Setting the log file location at runtime with a DOM configured log4net
6/15/2009 1:02 AM |
Perfect your site thanks.....
6/16/2009 3:12 AM |
thanks.ss
6/20/2009 5:42 AM |
thanks
# re: Setting the log file location at runtime with a DOM configured log4net
6/21/2009 6:43 AM |
Bensiz Günlerini Sen Unut Artik Seven Yüreginde Simdi Ben Varim Seni Böyle Sevmek Günahsa Eger Ben Anadan Dogma Günahkarim
# re: Setting the log file location at runtime with a DOM configured log4net
6/22/2009 1:38 AM |
then can make using progressive
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:12 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:12 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:13 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:14 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:15 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:15 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/1/2009 11:18 PM |
I'd like to congratulate you for this beautiful article was a really nice article nice article your health and your new look
# re: Setting the log file location at runtime with a DOM configured log4net
7/3/2009 1:57 AM |
seks sohbet good life