log4net in WPF application

zleug 61 Reputation points
2020-10-29T00:50:19.233+00:00

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

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,705 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. DaisyTian-1203 11,621 Reputation points
    2020-10-29T02:59:26.28+00:00

    You need to add [assembly: log4net.Config.XmlConfigurator(Watch = true)] in the AssemblyInfo.cs file. Like below picture shown:

    35863-capture.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Duane Arnold 3,216 Reputation points
    2020-10-30T06:08:02.33+00:00

    Yes, you're right. I also did it in UI layer. Just forgot to mention in the post. May be need to do it in another layer?

    This doesn't seem to be an optimal approach here. You are using a layered style with each layer vertically stacked. There should be global exception handling done in the WPF project, the presentation layer. Log4NET should be implemented in the WPF project used by the GEH. There is no need to have Log4NET implemented in the other layers. There should be no try/catches in any of the layers making any exception thrown an unhandled exception. The GEH catches all unhandled exception.

    There are numerous links using Bing or Google on how to implement GEH in a WPF project. There are also numerous links on how to implement Log4Net in WPF project.