Hi I try log expcetion on App Domain with NLog. It is WPF app with Caliburn Micro.
In MEF bootstraper I have this code:
static readonly ILog Log = LogManager.GetLog(typeof(NLogLogger));
#region Constructors
public MefBootStrapper()
: base()
{
_msgBox = new MessageBoxes();
_doHandle = true;
}
static MefBootStrapper()
{
LogManager.GetLog = type => new NLogLogger(type);
}
#endregion
#region Exception handling on App Domain
protected override void OnUnhandledException(object sender,
System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
if (_doHandle)
{
Log.Error(e.Exception.InnerException);
_msgBox.ShowException(e.Exception.InnerException);
e.Handled = true;
}
else
{
Log.Error(e.Exception.InnerException);
_msgBox.ShowException(e.Exception);
e.Handled = false;
}
}
#endregion
When I run app and throw exception from view modle class it show message box that is ok but exception is not logged to file.
I try log exception in view model calls:
something like this: Log.Error(new Exception(“4”));
This work, but If i try log exception in OnUnhandleException method it doesnt wokr. Why?
Your problem is that the static field
Loggets initialized before your static constructor runs see (Static field initialization). So you will have yourLogfield with the default Caliburn Nulloger initialized instead of your NLogLogger. You should move the Log field initialization into your static constructor.