Monday 24 May 2010

Log4net Settings in Web Project

To add log4net in web project steps are below:

1. Download and log4net dll in project where you want to use it.

2. We can write log in log file or we can send mail when an error occur. For these two options we have to make setup in Web.config file. Initially add following line after in web.config file.

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>

a. Write log in log file only, add following code in web.config file. You can change the log file name “PC.log.txt” is the file name here.

<log4net debug="true">

<appender name="RollingLogFileAppender

type="log4net.Appender.RollingFileAppender">

<file value="App_Data\PC.log.txt" />

<appendToFile value="true" />

<rollingStyle value="Size" />

<maxSizeRollBackups value="10" />

<maximumFileSize value="10MB" />

<staticLogFileName value="true" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%d [%t] %-5p %c - %m%n" />

layout>

appender>

<root>

<level value="DEBUG" />

<appender-ref ref="RollingLogFileAppender" />

root>

log4net>


b. Send log through mail need to add following line to web.config. In tag add e-mail address inside value. In tag create an account in gmail, add that e-mail address there. From all the error mail will be send. Put a mail subject in tag. In tag you may change the name but for that you have change the name in cs file also.

<log4net debug="true">

<appender name="SmtpAppender" type="log4netExtensions.SmtpClientSmtpAppender">

<to value="e-mail,e-mail" />

<from value="e-mail" />

<subject value="subject" />

<smtpHost value="smtp.gmail.com" />

<port value="587"/>

<authentication value="Basic" />

<bufferSize value="512" />

<lossy value="true" />

<evaluator type="log4net.Core.LevelEvaluator">

<threshold value="ALL"/>

evaluator>

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />

layout>

appender>

<logger name="PC.Web.Smtp">

<level value="All" />

<appender-ref ref="SmtpAppender" />

logger>

<root>

<level value="DEBUG" />

<appender-ref ref="RollingLogFileAppender" />

root>

log4net>


C. Combinly add both facility to web config add both appender in web.config file

<log4net debug="true">

<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">

<file value="App_Data\PC.log.txt" />

<appendToFile value="true" />

<rollingStyle value="Size" />

<maxSizeRollBackups value="10" />

<maximumFileSize value="10MB" />

<staticLogFileName value="true" />

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%d [%t] %-5p %c - %m%n" />

layout>

appender>

<appender name="SmtpAppender" type="log4netExtensions.SmtpClientSmtpAppender">

<to value="e-mail,e-mail />

<from value="e-mail" />

<subject value="subject" />

<smtpHost value="smtp.gmail.com" />

<port value="587"/>

<authentication value="Basic" />

<bufferSize value="512" />

<lossy value="true" />

<evaluator type="log4net.Core.LevelEvaluator">

<threshold value="ALL"/>

evaluator>

<layout type="log4net.Layout.PatternLayout">

<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />

layout>

appender>

<logger name="PC.Web.Smtp">

<level value="All" />

<appender-ref ref="SmtpAppender" />

logger>

<root>

<level value="DEBUG" />

<appender-ref ref="RollingLogFileAppender" />

root>

log4net>

3. Now Add below line in AssebmlyInfo.cs file of web project

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]

4. Add below line in cs file where you are going to use it.

a. To write log in log file:

protected static log4net.ILog _Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

b. To send mail:

protected static log4net.ILog _Logger = log4net.LogManager.GetLogger("PC.Web.Smtp");

Now _Logger object can be use. There are various method like Debug(), Info() etc.

12 comments:

  1. I want to to thank you for this fantastic read!! I certainly enjoyed every little bit of it.
    I have you bookmarked to look at new stuff you post…

    Feel free to visit my weblog email signature examples

    ReplyDelete
  2. Hey there terrific blog! Does running a blog similar to this
    require a massive amount work? I've very little knowledge of programming however I was hoping to start my own blog soon. Anyways, should you have any suggestions or techniques for new blog owners please share. I know this is off subject however I simply needed to ask. Cheers!

    my blog post email design templates

    ReplyDelete
  3. Generally I do not read post on blogs, but I wish to say that this write-up
    very pressured me to try and do so! Your writing style has been amazed me.
    Thank you, quite nice post.

    my web blog business email templates

    ReplyDelete
  4. Very descriptive article, I loved that a lot. Will there be a
    part 2?

    Feel free to surf to my page :: free email templates for outlook

    ReplyDelete
  5. Hmm it appears like your website ate my first comment (it was extremely long) so
    I guess I'll just sum it up what I had written and say, I'm thoroughly enjoying your blog.

    I as well am an aspiring blog writer but I'm still new to the whole thing. Do you have any helpful hints for novice blog writers? I'd certainly appreciate it.


    Stop by my page: bulk email marketing services

    ReplyDelete
  6. You're so awesome! I do not suppose I've truly read through a single thing like this before.
    So good to discover another person with some original thoughts on this subject matter.
    Really.. thank you for starting this up. This site is something that's needed on the internet, someone with a little originality!

    My page: assault

    ReplyDelete
  7. Every weekend i used to go to see this site, as i want enjoyment, since this this web
    site conations truly good funny information too.


    Take a look at my weblog - escalation

    ReplyDelete
  8. I was more than happy to find this web site. I want to to thank you for your time due
    to this fantastic read!! I definitely savored every bit of it and i also have you book marked to look
    at new things in your site.

    My website; How To Create Email Templates

    ReplyDelete
  9. Hi there I am so happy I found your web site, I really found you by accident, while I was searching on Yahoo
    for something else, Anyhow I am here now and would just like to say thanks a lot for a fantastic post and a all round thrilling blog
    (I also love the theme/design), I don’t have time to go through it all at the minute
    but I have bookmarked it and also included your RSS feeds, so when I have time I will be back to read much more, Please do keep
    up the fantastic jo.

    Feel free to visit my website ... riedell skates

    ReplyDelete
  10. I am genuinely glad to glance at this blog posts which carries tons of useful information,
    thanks for providing such data.

    my weblog explosion

    ReplyDelete
  11. I will right away take hold of your rss feed as I can't to find your e-mail subscription link or newsletter service. Do you've
    any? Kindly let me realize in order that I may subscribe. Thanks.


    Look at my homepage ... new.byelizabeth.Com/author/Tara0829

    ReplyDelete