none
Does not create log4net file if its referenced from another project C# application logging RRS feed

  • Question

  • Dear Experts,

    I have created a separate project which will maintain all datataccess, models,configurations and logging. I'm using log4net as logging tool. It works fine only if the Logger class file is within the project itself. I want to maintain logging in a different project so it can be used for other similar projects.

    1. I have windows form application "ClearningApp.UI" which contains mainly the application UI and separate class Library project "CLearningApp.Library" which contains DataAccess, Configuration, DatabaseSetup other utilities of the application. I have added reference CLearningApp.Library into CLearning.UI project

    2. I have to start logging everything from my application from UI to everything in library so I created a folder called Log inside CLeaningApp.Library and created a class Logger

    3. using System.Runtime.CompilerServices;
      
      namespace CLearningAppLibrary.Log
      {
          public class Logger
          {
              public static log4net.ILog GetLogger([CallerFilePath]string filename = "")
              {
                  return log4net.LogManager.GetLogger(filename);
              }
      
              public static readonly log4net.ILog log = GetLogger();
          }
      }
    1.  In the windows form application project "ClearningApp.UI", on app.config I have added the below log4net code.Logging into file using RollingfileAppender. 
    <configSections>
       <section name="Database" type="System.Configuration.NameValueSectionHandler" />
       <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler"/>
    </configSections>
      
      <Database>
        <add key="ClearningDB" value="Server=DESKTOP-AA\SQLDATA;Database=CLearningDB;Trusted_Connection=True;" />
      </Database>
    
      <log4net>
        <root>
          <level value="DEBUG"/>
          <appender-ref ref="LogFileAppender"/>
        </root>
        <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
          <param name="File" value="Clearning.exe.log"/>
          <param name="AppendToFile" value="true"/>
          <rollingStyle value="Size"/>
          <maxSizeRollBackups value="10"/>
          <maximumFileSize value="1000MB"/>
          <staticLogFileName value="true"/>
          <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%date [%thread] %C{1}.%M: %m%n"/>
          </layout>
        </appender>
      </log4net>
      
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
        </startup>

    1. I have added assembly code in the AssemblyInfo.cs of CLearningApp.UI
    [assembly: log4net.Config.XmlConfigurator(Watch = true)]
    1. In the program.cs file of CLearning.UI, I have the below code
    using CLearning.UI;
    using CLearningAppLibrary;
    using CLearningAppLibrary.Log;
    using System;
    using System.Windows.Forms;


    namespace CLearning { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool isServiceRunning = ConnectionStatusCheckerService.CheckSQLServiceStatus(); if(!isServiceRunning) { Logger.log.Error("SQL is not running"); } else { Application.Run(new MainForm()); Logger.log.Info("SQL is running, application started"); } } } }


    I have added project reference ClearningLibrary to ClearningApp.UI but the log file does not get created. I do not get any error on debug and the application starts but no log file nor the log. I have tried putting the logger class in the same UI project and it works but I want to separate it out.

    Please guide my mistake. Thank you

    NewB



    NewBInVB



    Tuesday, March 20, 2018 10:00 AM

Answers

  • The separte project/Library has other folder like Data Access and Configuration reference and it works fine.

    That is exactly what I mean: How will you log, when it doesn't?

    Create a sample solution showing this behavior. It looks like a misconfiguration due to the project boundary.

    • Marked as answer by NewBInCoding Tuesday, March 20, 2018 1:44 PM
    Tuesday, March 20, 2018 11:03 AM
  • https://haacked.com/archive/2006/01/13/SettingUpLog4NetForMultiLayeredApplications.aspx/

    You are using a Layered application. You can apply what is talked about in the link and just call Log4net in the layer as needed. You don't need a separate classlib project doing the logging, just set  reference to Log4net in the project the layer and use it.

    • Marked as answer by NewBInCoding Tuesday, March 20, 2018 1:44 PM
    Tuesday, March 20, 2018 11:13 AM

All replies

  • This idea is - well - suboptimal. Cause it means you cannot log anything, when your separate project has a problem.

    I would not do it.

    Tuesday, March 20, 2018 10:42 AM
  • The separte project/Library has other folder like Data Access and Configuration reference and it works fine. My concern is when I created the Log folder and created reference the log file is not getting created. 


    NewBInVB

    Tuesday, March 20, 2018 10:49 AM
  • The separte project/Library has other folder like Data Access and Configuration reference and it works fine.

    That is exactly what I mean: How will you log, when it doesn't?

    Create a sample solution showing this behavior. It looks like a misconfiguration due to the project boundary.

    • Marked as answer by NewBInCoding Tuesday, March 20, 2018 1:44 PM
    Tuesday, March 20, 2018 11:03 AM
  • https://haacked.com/archive/2006/01/13/SettingUpLog4NetForMultiLayeredApplications.aspx/

    You are using a Layered application. You can apply what is talked about in the link and just call Log4net in the layer as needed. You don't need a separate classlib project doing the logging, just set  reference to Log4net in the project the layer and use it.

    • Marked as answer by NewBInCoding Tuesday, March 20, 2018 1:44 PM
    Tuesday, March 20, 2018 11:13 AM