Intro
When an user creates a mistake in the configuration of NLog (like invalid XML), We (NLog) throw a NLogConfigurationException
. The exception contains the description what is wrong.
But sometimes this NLogConfigurationException
is "eaten" by a System.TypeInitializationException
if the first call to NLog is from a static field/property.
Example
E.g. if the user has this program:
using System;
using System.Collections.Generic;
using System.Linq;
using NLog;
namespace TypeInitializationExceptionTest
{
class Program
{
//this throws a NLogConfigurationException because of bad config. (like invalid XML)
private static Logger logger = LogManager.GetCurrentClassLogger();
static void Main()
{
Console.WriteLine("Press any key");
Console.ReadLine();
}
}
}
and there is a mistake in the config, NLog throws:
throw new NLogConfigurationException("Exception occurred when loading configuration from " + fileName, exception);
But the user will see:
"Copy exception details to the clipboard":
System.TypeInitializationException was unhandled Message: An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll Additional information: The type initializer for 'TypeInitializationExceptionTest.Program' threw an exception.
So the message is gone!
Questions
- Why is innerException not visible? (tested in Visual Studio 2013).
- Can I send more info to the
TypeInitializationException
? Like a message? We already sending an innerException. - Can we use another exception or are there properties on
Exception
so that more info is reported? - Is there another way to give (more) feedback to the user?
Notes
- of course we have no influence on the program written by the user.
- I'm one of the maintainers of NLog.
- Do you like to test it by yourself? Checkout https://github.com/NLog/NLog/tree/TypeInitializationException-tester and start NLog/src/NLog.netfx45.sln
Edit:
please note that I'm the library maintainer, not the user of the library. I cannot change the calling code!
InnerException
, andToString()
will also print the inner exception's message and stack trace. The only thing you could do is return a no-op logger so the user's app doesn't crash, but you'll get the obvious implications of that. – Lucas TrzesniewskiInnerException
null
? Also "Copy exception details to the clipboard" won't reveal the cause! (tested in DEBUG) – JulianInnerException
of typeNLogConfigurationException
with message:Error when setting property 'Layout' on NLog.Targets.DatabaseParameterInfo
, which itself has anInnerException
of typeArgumentException
with messageLayoutRenderer cannot be found: 'thisIsWrong'
– Lucas Trzesniewski