1

My program sends emails. And I'm giving the option to the user to test that it works. This program has 2 parts: A configuration window where the user set the configuration. It's executed with no arguments. And the sending message, that is executed in console with 3 arguments (@to, subject, message).

And I'm using log4net.

This is the code (in my Form). I'm making a call to the same program but with 3 arguments:

var to = inputBox.mailValue;
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = "Mailer.exe";     
processInfo.Arguments = to + " Test " + "\"This is a test message sent by Mailer.exe\""; 
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
proc.WaitForExit();
if(proc.ExitCode == 0)
    MessageBox.Show("A test message has been sent to " + to + ".\nCheck your inbox & mailerLOG.txt file", "Message sent", MessageBoxButtons.OK, MessageBoxIcon.Information); 
else if(proc.ExitCode < 0)
    MessageBox.Show("An error ocurred. Check mailerLOG.txt file for more details.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 

All works fine, the message is sent, but it doesn't record on my mailerLOG.txt When a message is sent, I do:

logger.Info("Message with subject <" + mail.Subject + "> has been sent to <" + mail.To + ">");

It neither records if an error ocurred.

What I'm missing?

My log4net config. (which it works properly on the rest of my program)

<configuration>
    <configSections>      

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

    <log4net>
      <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
        <target value="Console.Error" />
        <layout type="log4net.Layout.SimpleLayout" />
      </appender>
      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">  
        <file value="MailerLOG.txt"/>
        <appendToFile value="true"/>
        <rollingStyle value="Composite"/> 
        <datePattern value="yyyyMMdd"/>        
        <maxSizeRollBackups value="5"/>
        <maximumFileSize value="5MB"/>        
        <staticLogFileName value="true"/>
        <layout type="log4net.Layout.PatternLayout">  
          <conversionPattern value="[%date] [%-5level]: %message%newline"/>  
        </layout>
        <filter type="log4net.Filter.LevelRangeFilter">  
          <levelMin value="INFO"/>
        </filter>
      </appender>
      <root> 
        <level value="DEBUG"/>
        <appender-ref ref="ConsoleAppender" />
        <appender-ref ref="RollingFileAppender"/> 
      </root>         
    </log4net>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/></startup>
</configuration>
anat0lius
  • 2,145
  • 6
  • 33
  • 60
  • 4
    How are you configuring log4net? There are many ways to do this, and if it's not right, it won't log anything – James Thorpe Feb 05 '15 at 09:43
  • 4
    Your `if` and `else if` don't cover the case where `proc.ExitCode > 0`. Given that returning 1 is a common error indicator, I'd say you should try removing the second `if` and just making it `else` by itself. – anaximander Feb 05 '15 at 09:43
  • Can you copy and paste all the involved sections in the configuration of log4net? – dariogriffo Feb 05 '15 at 09:43
  • 1
    is log4net being configured in your main app, whilst you're trying to log within mailer.exe? mailer.exe is a separate process, and must have log4net configured within it as well if you wish to use it there (though you then need to start worrying about log file locking between processes) – James Thorpe Feb 05 '15 at 09:56
  • I'm making a call to the same program. – anat0lius Feb 05 '15 at 10:00
  • 2
    Then it's probably because the text file is locked. See [this question](http://stackoverflow.com/questions/1999382/intermittent-log4net-rollingfileappender-locked-file-issue). – James Thorpe Feb 05 '15 at 10:01

1 Answers1

2

This code here stands out to me:

if(proc.ExitCode == 0)
    MessageBox.Show("A test message has been sent to " + to + ".\nCheck your inbox & mailerLOG.txt file", "Message sent", MessageBoxButtons.OK, MessageBoxIcon.Information); 
else if(proc.ExitCode < 0)
    MessageBox.Show("An error ocurred. Check mailerLOG.txt file for more details.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

You don't cover the case where proc.ExitCode > 0, which is important, because many applications return 1 as their standard error code. Try removing the second if so that your code looks like this:

if(proc.ExitCode == 0)
    MessageBox.Show("A test message has been sent to " + to + ".\nCheck your inbox & mailerLOG.txt file", "Message sent", MessageBoxButtons.OK, MessageBoxIcon.Information); 
else
    MessageBox.Show("An error ocurred. Check mailerLOG.txt file for more details.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

Generally speaking, when logging errors you don't want to assume anything about what the program will do. If you need to know when something goes wrong, it's better to not guess what it'll look like when an error happens - instead, check to see whether it's the correct behaviour (which should be much easier to tell) and then have something that will catch everything else and log it, just in case.

anaximander
  • 7,083
  • 3
  • 44
  • 62