Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail target causes startup exception when using system mail settings #739

Closed
danjohnso opened this issue Jun 10, 2015 · 17 comments
Closed
Labels
bug Bug report / Bug fix mail-target
Milestone

Comments

@danjohnso
Copy link

Version: 4.0
Making a call to GetCurrentClassLogger with a mail target causes the following exception.
Workaround: Add smtpServer to config for NLog (to same value as the host parameter in the system settings)

NLog.NLogConfigurationException: Required parameter 'SmtpServer' on 'Mail Target[Mail_wrapped]' was not specified.   
 at NLog.Internal.PropertyHelper.CheckRequiredParameters(Object o)    
 at NLog.Config.LoggingConfiguration.ValidateConfig()    
 at NLog.Config.LoggingConfiguration.InitializeAll()    
 at NLog.LogFactory.get_Configuration()    
 at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey)    
 at NLog.LogFactory.GetLogger(String name)    
 at NLog.LogManager.GetCurrentClassLogger()    
2015-06-10 14:05:59.5612 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2015-06-10 14:05:59.5612 Debug Setting 'FileTarget.name' to 'File'
2015-06-10 14:05:59.5612 Debug Setting 'FileTarget.fileName' to '${basedir}/logs/log-${shortdate}.log'
2015-06-10 14:05:59.5768 Debug Setting 'FileTarget.archiveNumbering' to 'Date'
2015-06-10 14:05:59.5768 Debug Setting 'FileTarget.archiveEvery' to 'Day'
2015-06-10 14:05:59.5768 Debug Setting 'FileTarget.maxArchiveFiles' to '90'
2015-06-10 14:05:59.5768 Debug Setting 'FileTarget.layout' to '${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}'
2015-06-10 14:05:59.5768 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2015-06-10 14:05:59.5768 Debug Setting 'ExceptionLayoutRenderer.format' to 'tostring'
2015-06-10 14:05:59.5768 Debug Wrapping target 'File' with AsyncTargetWrapper and renaming to 'File_wrapped
2015-06-10 14:05:59.5768 Info Adding target AsyncWrapper Target[File](File Target[File_wrapped])
2015-06-10 14:05:59.5768 Debug Registering target File: NLog.Targets.Wrappers.AsyncTargetWrapper
2015-06-10 14:05:59.5768 Debug Setting 'UppercaseLayoutRendererWrapper.uppercase' to 'true'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.name' to 'Mail'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.html' to 'true'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.from' to 'mail@mail.com'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.to' to 'mail@mail.com'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.useSystemNetMailSettings' to 'true'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.subject' to 'Portal Error'
2015-06-10 14:05:59.5768 Debug Setting 'MailTarget.body' to 'Time: ${longdate}${newline}Message:${newline}${message}${newline}Exception:${newline}${exception:format=tostring}'
2015-06-10 14:05:59.5768 Debug Setting 'ExceptionLayoutRenderer.format' to 'tostring'
2015-06-10 14:05:59.5768 Debug Wrapping target 'Mail' with AsyncTargetWrapper and renaming to 'Mail_wrapped
2015-06-10 14:05:59.5768 Info Adding target AsyncWrapper Target[Mail](Mail Target[Mail_wrapped])
2015-06-10 14:05:59.5768 Debug Registering target Mail: NLog.Targets.Wrappers.AsyncTargetWrapper
2015-06-10 14:05:59.5768 Info Configured from an XML element in D:\sites\portal\web.config...
2015-06-10 14:05:59.5768 Debug --- NLog configuration dump. ---
2015-06-10 14:05:59.5768 Debug Targets:
2015-06-10 14:05:59.5768 Info AsyncWrapper Target[File](File Target[File_wrapped])
2015-06-10 14:05:59.5768 Info AsyncWrapper Target[Mail](Mail Target[Mail_wrapped])
2015-06-10 14:05:59.5768 Debug Rules:
2015-06-10 14:05:59.5924 Info logNamePattern: (:All) levels: [ ] appendTo: [ Mail ]
2015-06-10 14:05:59.5924 Info logNamePattern: (:All) levels: [ ] appendTo: [ File ]
2015-06-10 14:05:59.5924 Debug --- End of NLog configuration dump ---
2015-06-10 14:05:59.5924 Info Watching path 'D:\sites\portal\' filter 'web.config' for changes.
2015-06-10 14:05:59.5924 Info Found 39 configuration items
2015-06-10 14:06:02.6656 Debug Targets for Class by level:
2015-06-10 14:06:02.6656 Debug Trace =>
2015-06-10 14:06:02.6812 Debug Debug => File
2015-06-10 14:06:02.6812 Debug Info => File
2015-06-10 14:06:02.6812 Debug Warn => File
2015-06-10 14:06:02.6812 Debug Error => File
2015-06-10 14:06:02.6812 Debug Fatal => Mail File
2015-06-10 14:06:02.6968 Debug Targets for Class by level:
2015-06-10 14:06:02.6968 Debug Trace =>
2015-06-10 14:06:02.6968 Debug Debug => File
2015-06-10 14:06:02.6968 Debug Info => File
2015-06-10 14:06:02.6968 Debug Warn => File
2015-06-10 14:06:02.6968 Debug Error => File
2015-06-10 14:06:02.6968 Debug Fatal => Mail File
@304NotModified
Copy link
Member

Can you post your config? It's with a wrapper I see?

@danjohnso
Copy link
Author

<mailSettings>
      <smtp deliveryMethod="Network" from="mail@mail.com">
        <network host="SMTP" port="25" defaultCredentials="true" />
      </smtp>
</mailSettings>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
    <targets async="true">
      <target name="File" xsi:type="File" fileName="${basedir}/logs/log-${shortdate}.log" archiveNumbering="Date" archiveEvery="Day" maxArchiveFiles="90" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}" />
      <target name="Mail" xsi:type="Mail" html="true" from="mail@mail.com" to="mail@mail.com" useSystemNetMailSettings="true" subject="Portal Error" body="Time: ${longdate}${newline}Message:${newline}${message}${newline}Exception:${newline}${exception:format=tostring}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Fatal" writeTo="Mail" />
      <logger name="*" minlevel="Debug" writeTo="File" />
    </rules>
  </nlog>

@304NotModified
Copy link
Member

Can you try without async and without the file target?

@304NotModified
Copy link
Member

This is the config with the work around? Can you post the other one also?

@danjohnso
Copy link
Author

I removed the async and file target, same result. The config I posted above is the broken config, here is the workaround config:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true">
    <targets async="true">
      <target name="File" xsi:type="File" fileName="${basedir}/logs/log-${shortdate}.log" archiveNumbering="Date" archiveEvery="Day" maxArchiveFiles="90" layout="${longdate}|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}" />
      <target name="Mail" xsi:type="Mail" smtpServer="SMTP" html="true" from="mail@mail.com" to="mail@mail.com" useSystemNetMailSettings="true" subject="Portal Error" body="Time: ${longdate}${newline}Message:${newline}${message}${newline}Exception:${newline}${exception:format=tostring}" />
    </targets>
    <rules>
      <logger name="*" minlevel="Fatal" writeTo="Mail" />
      <logger name="*" minlevel="Debug" writeTo="File" />
    </rules>
  </nlog>

@304NotModified
Copy link
Member

Thanks! We try to fix this soon.

@304NotModified 304NotModified added this to the 4.0.1 milestone Jun 10, 2015
@304NotModified 304NotModified added the bug Bug report / Bug fix label Jun 10, 2015
@304NotModified
Copy link
Member

Just checking

This is conform the specs?https://github.com/nlog/NLog/wiki/Mail-target

@danjohnso
Copy link
Author

Yeah, it used to work in 3.2.0.0, I just upgraded to 4.0 and it broke. Documentation looks ok and as I understood it, if I set useSystemNetMailSettings="true", then those values are used unless explicitly overriden in the target properties

@304NotModified
Copy link
Member

OK we will check what has changed since 3.2.0

@304NotModified
Copy link
Member

I see this has been changed indeed in 4.0

#523

Add RequiredParameterAttribute to the SmtpServer property - it should be required according to the documentation. Breaking change?

@304NotModified
Copy link
Member

So I agree, this breaking change is unneeded and unwanted.

This will be fixed in 4.0.1

@304NotModified
Copy link
Member

Thanks for reporting! 👍

@danjohnso
Copy link
Author

This is still not fixed in 4.0.1, 4.1 or 4.1.1. Same error, same trace

@304NotModified
Copy link
Member

I will test this with an unit test.

@304NotModified
Copy link
Member

I tested it with

        [Fact]
        public void MailTargetInitialize_WithoutSpecifiedSmtpServer_should_not_ThrowsConfigException2()
        {
            var config = @"<nlog throwExceptions='true' autoReload='true'>
  <targets async='true'>
    <target name='m1' type='Mail'  html='true' 
            from='mail@mail.com' to='mail@mail.com' useSystemNetMailSettings='true' subject='Portal Error' 
            body='Time: ${longdate}${newline}Message:${newline}${message}${newline}Exception:${newline}${exception:format=tostring}' />
  </targets>
  <rules>
    <logger name='*' minlevel='Debug' writeTo='m1' />
  </rules>
</nlog>";

            var configuration = CreateConfigurationFromString(config);

            LogManager.Configuration = configuration;

            var logger = LogManager.GetCurrentClassLogger();
            logger.Debug("test");



        }

And it just works.

It seems that the update has failed on your site? Are there more projects in your solution with other versions of NLog?

@danjohnso
Copy link
Author

Woops, found an old version in the server GAC that was causing problems. Looks like it is working correctly

@304NotModified
Copy link
Member

OK good to hear it has been solved. I was also thinking is a good idea to log the version of NLog to the internal log :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report / Bug fix mail-target
Projects
None yet
Development

No branches or pull requests

2 participants