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

Logging doesn't work if mail target added to NLog configuration for web application #1407

Closed
dimrun opened this issue Apr 27, 2016 · 9 comments
Labels
bug Bug report / Bug fix mail-target
Milestone

Comments

@dimrun
Copy link

dimrun commented Apr 27, 2016

Type: Bug

NLog version: 4.3.2

Platform: .Net 3.5 / .Net 4.0 / .Net 4.5 / Mono 4 / Silverlight 4 / Silverlight 5

Current NLog config (xml or C#, if relevant):
nothing special, just mail target added to web.config

In case of a BUG:

  • What is the current result?
    Exception occurs:
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: exePath must be specified when not running inside a stand alone exe.
  at System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath, Boolean preLoad)
  at System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel userLevel)
  at NLog.Targets.MailTarget.get_MailSettings()
  • What is the expected result?
    No errors
  • Stack trace:
---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: exePath must be specified when not running inside a stand alone exe.
  at System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap, Boolean isMachine, ConfigurationUserLevel userLevel, String exePath, Boolean preLoad)
  at System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel userLevel)
  at NLog.Targets.MailTarget.get_MailSettings()
  --- End of inner exception stack trace ---
  at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
  at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
  at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
  at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
  at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, HashSet`1 visitedObjects)
  at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, HashSet`1 visitedObjects)
  at NLog.Internal.ObjectGraphScanner.ScanPropertiesList[T](List`1 result, IEnumerable`1 elements, Int32 level, HashSet`1 visitedObjects)
  at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, HashSet`1 visitedObjects)
  at NLog.Internal.ObjectGraphScanner.FindReachableObjects[T](Object[] rootObjects)
  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.GetLogger(String name)

Possible reason / fix:

When we read SMTP configuration (MailTarget.MailSettings property) we don't take into account that it can be executed in scope of web application

Current code:

 _currentailSettings = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).GetSection("system.net/mailSettings/smtp") as SmtpSection;

A special switch for this case should be added. Something like this:
http://stackoverflow.com/questions/5629209/how-to-replace-openexeconfiguration-in-a-web-context-asp-net-mvc-1

@dimrun dimrun changed the title Logging doesn't wok if mail target added to NLog configuration for web application Logging doesn't work if mail target added to NLog configuration for web application Apr 27, 2016
@ivanpointer
Copy link

ivanpointer commented Apr 27, 2016

I don't believe this problem existed in 4.3.1, and appears to have been introduced with 4.3.2. Our organization is sticking with 4.3.1 until this is straightened out.

@ivanpointer
Copy link

This also includes WCF applications (I know, they're technically "web" applications).

@304NotModified 304NotModified added the bug Bug report / Bug fix label Apr 27, 2016
@304NotModified 304NotModified added this to the 4.3.3 milestone Apr 27, 2016
@304NotModified
Copy link
Member

304NotModified commented Apr 27, 2016

This bug is indeed introduced in 4.3.2. Will fix this in 4.3.3. Any idea how to unit test this?

bug introduced in #1402

@304NotModified
Copy link
Member

I think the code in http://stackoverflow.com/questions/5629209/how-to-replace-openexeconfiguration-in-a-web-context-asp-net-mvc-1 is problematic as it uses system.web, and we don't want a dependency to system.web from NLog. Any thoughts @ivanpointer ?

@krazyest
Copy link

note that if smtpDeliveryMethod="network" is used, it mitigates the issue and the exePath exception is not shown, but Nlog does not log anymore

@304NotModified
Copy link
Member

ATM I'm working on a fix

@304NotModified
Copy link
Member

This is fixed in NLog 4.3.3. Also the code is more robust - no crash or disabled logging when there are troubles reading the "from" from the SMTP config.

https://www.nuget.org/packages/NLog/4.3.3

@ivanpointer
Copy link

Thank you! I just now saw the notes and will pull down 4.3.3 to verify.

Thanks again!

@304NotModified
Copy link
Member

👍

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

4 participants