Hi All.
I'm trying to log exceptions in wpf application. For that I installed log4net package and modified app.config file
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception" />
</layout>
</appender>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="LogFile.log" />
<appendToFile value="true" />
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="ERROR" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
In common layer I created Logger class with code
public static class Logger
{
static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static void Log(Exception ex, string info="", bool isWarning = false)
{
if (isWarning)
ThreadPool.QueueUserWorkItem(temp => log.Warn(info, ex));
else
ThreadPool.QueueUserWorkItem(temp => log.Error(info, ex));
}
}
In data layer I have class with method that populate DataGrid in UI
public static IEnumerable<EmployeeDT> GetEmployeeList(int employeeid)
{
try
{
using (var context = new Model1())
{
*cofigure to retrieve data*
return employeedg.ToArray();
}
}
catch (Exception ex)
{
Logger.Log(ex);
throw new Exception("The data is invalid. " + ex.Message);
}
}
The UI layer has reference to common layer. When I click button to open form with populated DataGrid and have exception condition I didn't get console with error message and the log file also not created.
How to fix the problem?
Thanks