Click here to Skip to main content
14,774,584 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an asp.net site. I have configured the log4net for the logging. I am using only two provider File and SMTP. I want to log all type of level in the File, but email should be send only for the the Error level. Do not want to send an email for the ERROR, INFO, WARN and FATAL. I have used the following configuration in the web.config file.

<configSections>
		<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
	</configSections>
	<log4net>
		<appender name="LogFileAppender" type="log4net.Appender.FileAppender">
      <threshold value="ALL" />
			<param name="File" value="D:\Log4NetWeb\LogFile.txt"/>
			<param name="AppendToFile" value="true"/>
			<layout type="log4net.Layout.PatternLayout">
				<param name="Header" value="[Header]\r\n"/>
				<param name="Footer" value="[Footer]\r\n"/>
				<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n"/>
			</layout>
		</appender>
		
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
			<to value="maymailaddress@yahoo.com"/>
			<from value="ttext@test.com"/>
			<from value="text2@test.com"/>
			<!--<subject value="test logging message"/>-->
      <subject type="log4net.Util.PatternString" value="%property{log4net:HostName} Error: %appdomain" />
			<smtpHost value="SMTP.myserver.com"/>
			<bufferSize value="512"/>
      <lossy value="true"/>
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="Error"/>
      </evaluator>
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline"/>
			</layout>
		</appender>
		<root>
			<!--<level value="ALL"/>-->
			<appender-ref ref="SmtpAppender"/>
      <appender-ref ref="LogFileAppender"/>
		</root>

    <!--<logger name="File">
      <level value="All" />
      <appender-ref ref="LogFileAppender" />
    </logger>
    <logger name="SmtpAppender">
      <level value="ERROR" />
      <appender-ref ref="SmtpAppender" />
    </logger>-->

  </log4net>



and using the following code for the logging

protected void Page_Load(object sender, EventArgs e)
   {
       DOMConfigurator.Configure();

       try
       {
           int a = 0, b = 1;

           logger.Debug("Log Debug");
           logger.Info("Log info");
           logger.Warn("Log Warn");
           logger.Fatal("Log Fatal");


           float c = b/a;

       }
       catch (Exception ex)
       {
           logger.Error("some errro in the application ",ex);
       }

   }



However I am getting email for every log. How can I configure the log only for the ERROR? So that I will get email only for the ERROR.
Posted

<log4net>
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="log\log.txt" />
      <appendtofile value="true" />
      <datepattern value="yyyyMMdd" />
      <rollingstyle value="Date" />
      <level value="ALL" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionpattern value="%date [%thread] %-5level %logger [%property] - %message%newline"></conversionpattern>
      </layout>
    </appender>
    <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net">
      <to value="to@email.com" />
      <from value="from@email.com" />
      <subject value="LOG4NET Email TESTING" />
      <smtphost value="Host Name" />
      <bufferSize value="1"/>
      <lossy value="true"/>
      <evaluator type="log4net.Core.LevelEvaluator,log4net">
        <threshold value="ERROR"/>
      </evaluator>
      <layout type="log4net.Layout.PatternLayout,log4net">
        <conversionpattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" />
      </layout>
    </appender>
    <root>
      <appender-ref ref="RollingFile" />
      <appender-ref ref="SmtpAppender" />
    </root>

  </log4net>
   
Comments
Michal Rezler 8-Oct-13 8:30am
   
Great, it works.
Many thanks
I am not a log4net expert, but I believe your problem is due to you having set threshold value="ALL". Try setting threshold value="ERROR". Also take a look at this article: http://www.webonweboff.com/tips/asp/logging.aspx[^]
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900