Log4net used in WebServices.

Source: Internet
Author: User
Tags log4net

Transferred from: http://www.cnblogs.com/geqinggao/archive/2013/02/01/Gerry.html

Recently wrote a webservices to record log, the first when the thought of course is log4net.dll, I put the original Winfrom configuration copy come, log can't remember, well problem?

So Baidu, Google, a lot of articles, some said webservices can not use log4net, and some posted configuration operation or can not remember, elder brother to interest, log4net not so not to force it? A study of the answer is negative, log4net is still very strong.

There are several places to configure log4net for use on webservices:

1. Configure the Web. config

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
<section name= "log4net" type= "log4net. Config.log4netconfigurationsectionhandler,log4net "/>
</configSections>
<log4net>
<appender name= "Errorrollingfileappender" type= "log4net. Appender.rollingfileappender,log4net ">
<param name= "File" value= "Logs/error.log"/>
<param name= "Appendtofile" value= "true"/>
<param name= "Rollingstyle" value= "Date"/>
<param name= "Datepattern" value= "yyyy. Mm.dd "/>
<param name= "Staticlogfilename" value= "true"/>
<filter type= "log4net. Filter.levelrangefilter ">
<acceptonmatch value= "true"/>
<levelmin value= "ERROR"/>
<levelmax value= "FATAL"/>
</filter>
<maximumfilesize value= "15MB"/>
<layout type= "log4net. Layout.patternlayout,log4net ">
<param name= "Conversionpattern" value= "%newline%d [%t]%-5p%c:%m%n"/>
</layout>
</appender>
<appender name= "Inforollingfileappender" type= "log4net. Appender.rollingfileappender,log4net ">
<param name= "File" value= "Logs/info.log"/>
<param name= "Appendtofile" value= "true"/>
<param name= "Rollingstyle" value= "Date"/>
<param name= "Datepattern" value= "yyyy. Mm.dd "/>
<param name= "Staticlogfilename" value= "true"/>
<filter type= "log4net. Filter.levelrangefilter ">
<acceptonmatch value= "true"/>
<levelmin value= "DEBUG"/>
<levelmax value= "WARN"/>
</filter>
<maximumfilesize value= "15MB"/>
<layout type= "log4net. Layout.patternlayout,log4net ">
<param name= "Conversionpattern" value= "%newline%d [%t]%-5p%c:%m%n"/>
</layout>
</appender>
<root>
<priority value= "DEBUG"/>
<appender-ref ref= "Errorrollingfileappender"/>
<appender-ref ref= "Inforollingfileappender"/>
</root>
</log4net>
</configuration>

=====================

Well, how do you use it? I'm sure everyone will.

Add the following to the. cs file:

private static ILog log = Logmanager.getlogger (System.Reflection.MethodBase.GetCurrentMethod (). DeclaringType);

In the constructor of the. cs file, add the

Xmlconfigurator.configure ();

You can then call it in the following way:

Log. Info ("Log info."); /recorded in Logs/info.log
Log. Error ("Log Error."); /recorded in Logs/error.log
Log. Fatal ("Log Fatal."); /recorded in Logs/error.log
Log. Debug ("Log Debug."); /recorded in Logs/info.log

2. Configure permissions

I was unable to write log because I did not configure IIS_IUSRS permissions. If your Web. config is configured according to step 1th, if you can't remember log, right-click your Webservices→edit Permission in IIS ... →security Find your Iis_iusrs click Edit ... Tick the full control and apply.

If you have completed the above two steps, congratulations on your Logs folder will magically appear.

Little doubt, the above picture in the error message is separated by the day, but Info.log is together. I have the same configuration in Windows Services ' App. Config, and Info.log is separated daily,

Don't know if this is the difference between Web services and window services?

(turn) log4net used in WebServices

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.