Receiving errors by email

Topics: Administration
Mar 28, 2013 at 5:54 PM
Hey everyone,

Today I ran into trouble with my orchard website.

Seems that the emailing wasn't working anymore due to an inactive outlook.com account.

This was already going on for a few days so I received a call from the person who receives the info mails.

Now I was wondering if their is a way or a module out there that can send out errors logged into \App_Data\Logs folder.

For the moment I can only ftp into them but it would be nice to receive these by email to stay on top of problems.

Kind regards,

Borrie
Developer
Mar 29, 2013 at 12:05 AM
Orchard uses Log4net, which supports all sorts of appenders, including SMTP appenders. you can configure the appender to use in Config/Log4net.config. I don't know the details from the top of my head, but Google knows all about it.
Mar 29, 2013 at 10:36 AM
Skywalker,

Thanks again for your usefull info.

I've added it to the log4net.config file, can you verify if this is corret?:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
  <!-- 
  If you are looking here and want more output, 
  first thing to do is change root/priority/@value to "INFO" or "ALL" 
  -->
  
  <root>
    <!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
    <priority value="WARN" />

    <appender-ref ref="error-file" />
    <appender-ref ref="debug-file" />
  </root>

  <logger name="Orchard">
    <!-- messages coming from orchard are provided to the attached debugger -->
    <appender-ref ref="debugger"/>

    <!-- 
    note: if you put a ref=debugger into root above and widen the priority to ALL or DEBUG,
    then you will see nhibernate trace in the attached debugger as well
    -->
  </logger>

  <logger name="Orchard.Localization">
    <!-- this source is very verbose - setting priority here to avoid flooding trace if root priority is lowered -->
    <priority value="WARN" />
  </logger>
  
  <logger name="NHibernate.Cache">
    <!-- this source is very verbose - setting priority here to avoid flooding trace if root priority is lowered -->
    <priority value="ERROR" />
  </logger>

  <!-- example of turning on the output from a component or namespace-->
  <!--
    <logger name="Orchard.Data.SessionLocator">
      <priority value="INFO" />
    </logger>
  -->

  <appender name="SMTPAppender" type="log4net.Appender.SMTPAppender">
      <authentication value="Basic" />
      <to value="xx@yy.be" />
      <from value="yy@xx.be" />
      <username value="user" />
      <password value="password" />
      <subject value="ERROR" />
      <smtpHost value="smtp.sendgrid.net" />
      <port value="587" />
      <lossy value="true" />
      <evaluator type="log4net.Core.LevelEvaluator">
        <threshold value="WARN" />
      </evaluator>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline%date [%thread] %-5level %logger %newline %message%newline%newline%newline" />
      </layout>
    </appender>
    <root>
      <level value="ERROR"></level>
    </root>
    <logger name="SMTPAppender">
      <level value="WARN"></level>
      <appender-ref ref="SMTPAppender"></appender-ref>
    </logger>
  
  <appender name="debugger" type="log4net.Appender.DebugAppender">
    <!-- debugger: visual studio, if attached -->
    
    <immediateFlush value="true" />
    <layout type="log4net.Layout.SimpleLayout" />
  </appender>

  <appender name="debug-file" type="Orchard.Logging.OrchardFileAppender">
    <!-- debug log: all messages, based on logger priority settings of namespaces above -->
    
    <file value="App_Data/Logs/orchard-debug" />
    <appendToFile value="true" />

    <!-- immediate flush on error log, to avoid data loss with sudden termination -->
    <immediateFlush value="true" />

    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />
    <!-- prevent orchard.exe from displaying locking debug messages -->
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %logger - %message%newline" />
    </layout>
  </appender>

  <appender name="error-file" type="Orchard.Logging.OrchardFileAppender">
    <!-- error log: only ERROR and FATAL subset of debug log -->
    
    <file value="App_Data/Logs/orchard-error" />
    <appendToFile value="true" />

    <!-- immediate flush on error log, to avoid data loss with sudden termination -->
    <immediateFlush value="true" />

    <staticLogFileName value="false" />
    <rollingStyle value="Date" />
    <datepattern value="-yyyy.MM.dd'.log'" />

    <!-- prevent orchard.exe from displaying locking error messages -->
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />

    <filter type="log4net.Filter.LevelRangeFilter">
      <!-- only error and fatal messages end up in this target, even if child loggers accept lower priority -->
      <levelMin value="ERROR" />
    </filter>

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%date [%thread] %logger - %message%newline" />
    </layout>
  </appender>
</log4net>
The error I got from the email malfunctioning in the logs was this:
2013-03-28 16:17:27,251 [29] Orchard.Email.Services.EmailMessagingChannel - An unexpected error while sending a message to xx@yy.be: OForm_Form_Result
System.Net.Mail.SmtpException: Mailbox unavailable. The server response was: 5.3.4 Requested action not taken; To continue sending messages, please sign in to your account.
   at System.Net.Mail.DataStopCommand.CheckResponse(SmtpStatusCode statusCode, String serverResponse)
   at System.Net.Mail.DataStopCommand.Send(SmtpConnection conn)
   at System.Net.Mail.SmtpConnection.OnClose(Object sender, EventArgs args)
   at System.Net.ClosableStream.Close()
   at System.Net.Mail.MailWriter.Close()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at Orchard.Email.Services.EmailMessagingChannel.SendMessage(MessageContext context)
Do you think the appender will catch this if it happens again?

Kind regards,

Borrie
Developer
Mar 29, 2013 at 5:18 PM
The appender definition looks fine, but you will have to try it out to be sure.
Also, don't forget to actually append the appender to the <root> element, like this:
<root>
    <!-- Value of priority may be ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF -->
    <priority value="WARN" />

    <appender-ref ref="error-file" />
    <appender-ref ref="debug-file" />
    <appender-ref ref="SMTPAppender" />
  </root>
Provided that you setup SMTPAppender correctly and that the SMTP server is up, then yes, you should receive the same exceptions being logged to the text files by email.
Mar 29, 2013 at 5:22 PM
Skywalker,

Thanks again, i've made the changes, unfortunately No errors where logged today so I don't know if it works :) :)

Borrie
Developer
Mar 29, 2013 at 5:29 PM
No errors huh? How unfortunate ;)
Mar 29, 2013 at 5:35 PM
It's just one of those days... :)

I'll keep you posted when I get one :)
Apr 3, 2013 at 9:56 AM
Skywalker,

After days of not getting any errors (it's a sad world) I decided to test it in my development environment. I've put a fake password in my email settings and I received the errors by email!

It's working, thanks again for the advice, this will save me a lot of headachs in the future!

Borrie
Developer
Apr 3, 2013 at 10:22 AM
Edited Apr 3, 2013 at 10:22 AM
No problem. Glad to hear you got it to work. Enjoy the error reports!
Apr 3, 2013 at 10:23 AM
Never in my life i've been so happy to receive errors :)