A few days ago wrote a Windows service program, where the use of log4net has always failed.
Regardless of log4net configuration written in the app.config, or stand-alone config file, is the same.
Today, I finally found out that the service was not properly positioned in this configuration file.
So after trying, it turns out that the key is to get the directory of the current EXE in the code below and use it to get the config file path. You can print the log correctly by this notation:
public class Service1:servicebase
{
The main entry point of the process
private static void Main ()
{
String assemblyfilepath = assembly.getexecutingassembly (). Location;
String assemblydirpath = Path.getdirectoryname (Assemblyfilepath);
String ConfigFilePath = Assemblydirpath + "//log4net.config";
Domconfigurator.configureandwatch (New FileInfo (ConfigFilePath));
Servicebase[] ServicesToRun;
ServicesToRun = new servicebase[] {new Service1 ()};
Servicebase.run (ServicesToRun);
}
Other slightly
}
The other is not much written, with my log4net.config:
? XML version= "1.0" encoding= "Utf-8"?>
< log4net >
< Appender name = "Rollinglogfileappender" type = log4net. Appender.rollingfileappender ">
< file value = "Logs/log.txt"/>
< Appendtofile value = "true"/>
< Rollingstyle value = "Size"/>
< maxsizerollbackups value = "Ten"/>
< maximumFileSize value = "100KB"/>
< Staticlogfilename value = "true"/>
< layout type = "Log4net." Layout.patternlayout ">
< Conversionpattern value = "%d [%t]%-5p%c [%x]-%m%n"/>
</Layout >
</Appender >
< root >
< level value = ' All '/>
< Appender-ref ref = "Rollinglogfileappender"/>
</root >
</log4net >
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.