Dynamically modifying the log file name of the Log4net component

Source: Internet
Author: User

Registered bloggers are also a long time, which is an opening article.

Recent project use to Log4net to log, of course apart first to Cnblogs to see the master on Log4net tutorials and experience
The main reference to the Log4net use guide (indeed a very good log4net guide), in the comments in this article @ Solomon has a
The problem may be that the MO is more busy without time to be an experiment and a solution, the questions are as follows:
If I do not set Appender file in my profile, I delete the <param name= "File" value= "Log-file.txt"/> One line of information,
I want to dynamically set the log log file name (such as a date file name) when the program is running, and what to do to achieve
Just as the project also needs to dynamically change the log file name, try to track the Log4net log process and see if it's written in the log.
Log4net was found in the tracking project. The Repository.Hierarchy.Logger class has the Appenders attribute, which is to return all current append
Collection, just through the debug trace to discover Log4net. ILog log = log4net. Logmanager.getlogger ("Applogger"); The log created is a log4net. Core.logimpl type, and there is a Logger property in this type just fine Log4net.Repository.Hierarchy.Logger
Type, so take for granted the following function to modify the log path 1 private void Changelog4netlogfilename (log4net. ILog ILog, String fileName)
2 {
3 log4net. Core.logimpl Logimpl = ILog as log4net. Core.logimpl;
4 if (Logimpl!= null)
5 {
6 log4net. appender.appendercollection AC = (log4net. Repository.Hierarchy.Logger) Logimpl.logger). Appenders;
7 for (int i = 0; i < AC. Count;i + +)
8 {//Here I only make changes to the Rollingfileappender type
9 log4net. Appender.rollingfileappender rfa = Ac[i] as log4net. Appender.rollingfileappender;
Ten if (RFA!= null)
11 {
RfA. File = FileName;
if (! System.IO.File.Exists (FileName))
14 {
System.IO.File.Create (FileName);
16}
17}
18}
19}
OK, let's test
private void Testchangelog4netlogfilename ()
{
String fileName = @ "C:/chang.log";
Log4net. ILog ILog = log4net. Logmanager.getlogger ("Applogger");

Changelog4netlogfilename (ILog, fileName);
Ilog.info ("Test:info");
Run, oh C packing directory does not have to expect the same as did not appear chang.log file. Why guess Rollingfileappender at the time of initialization
Fixed the file name, late changes to the file name does not work. Continue to trace the code and find that Rollingfileappender is used directly when logging
A quiettextwriter type of private variable M_QTW (directly inheriting from the base class Textwriterappender) that is instantiated when initialized, continues to trace the discovery
The base class (Textwriterappender) provides the writer property to modify M_QTW values, all of which modify the Changelog4netlogfilename function above
1 private void Changelog4netlogfilename (log4net. ILog ILog, String fileName)
2 {
3 log4net. Core.logimpl Logimpl = ILog as log4net. Core.logimpl;
4 if (Logimpl!= null)
5 {
6 log4net. appender.appendercollection AC = (log4net. Repository.Hierarchy.Logger) Logimpl.logger). Appenders;
7 for (int i = 0; i < AC. Count;i + +)
8 {//Here I only make changes to the Rollingfileappender type
9 log4net. Appender.rollingfileappender rfa = Ac[i] as log4net. Appender.rollingfileappender;
Ten if (RFA!= null)
11 {
RfA. File = FileName;
if (! System.IO.File.Exists (FileName))
14 {
System.IO.File.Create (FileName);
16}
17//Update writer properties
RfA. Writer = new System.IO.StreamWriter (RFA. File,rfa. Appendtofile,rfa. Encoding);
19}
20}
21}
22} In running the above test code, Ok,chang.log file appears as desired.
Write blogs for the first time, Rorillo a big paragraph, hope will not challenge your patience ^_^

Finally, attach the Log4net configuration (also directly from the cnblogs on the above copy down)
1.? XML version= "1.0" encoding= "Utf-8"?>
2 < configuration >
3 <!--The configsections section is not required if you do not use App.config as a configuration file. -->
4 < configsections >
5 <!--The full format of the "type" attribute is: Configuration section processor class name, assembly name, version= assembly version number, culture= zone information, publickeytoken= public key-->
6 < section name = "Log4net" type = "log4net." Config.log4netconfigurationsectionhandler,log4net "/>
7 </configsections >
8 < Log4net >
9 <!--Logger logger, can have multiple-->
< logger name = "Applogger" >
< level value = ' All '/>
< Appender-ref ref = "Rollinglogfileappender"/>
< Appender-ref ref = "Consoleappender"/>
</Logger >
< logger name = "Form1" >
< level value = "DEBUG"/>
< Appender-ref ref = "Logfileappender"/>
</Logger >
<!--the base of all logger, the root setting works in all logger.
20 when you repeatedly set the same appender in root and logger, you will notice that the same log information will be logged two times. -->
<!--<root>
<level value= "WARN"/>
23

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.