Base solution for your next web application
Open Closed

log4net not logging #1937


User avatar
0
kythor created

Hi,

since working with aspnetzero I have not touched any configuration about Log4Net. But there are no log files in the Logs folder. What do I have to do?

I have added this line in the main HomeController:

Logger.Debug("HomeController - Test: " + Clock.Now.ToString());

but still no log files are generated.


6 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Is it in development or production? Are you using AspNet Core or AspNet MVC 5.x?

    It should be normally in App_Data\Logs. If it does not write, please be sure that your app has Write permission to that folder.

  • User Avatar
    0
    kythor created

    I'm using MVC 5.x. The folder App_Data/Logs didn't even exist. So I created it and gave the IUSR write rights. But still nothing is added.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The log4net.config file's content should be like this by default.

    <?xml version="1.0" encoding="utf-8" ?>
    <log4net>
      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
        <file value="App_Data/Logs/Logs.txt" />
        <encoding value="utf-8" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10000KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
        </layout>
      </appender>
      <root>
        <appender-ref ref="RollingFileAppender" />
        <level value="DEBUG" />
      </root>
    </log4net>
    

    and it's configured in Global.asax file's Application_Start method like this:

    //Log4Net configuration
    AbpBootstrapper.IocManager.IocContainer
        .AddFacility<LoggingFacility>(f => f.UseAbpLog4Net()
            .WithConfig("log4net.config")
        );
    

    Can you check those two sections as well ?

  • User Avatar
    0
    kythor created

    back trying to get logging to work. This is what I have:

    //Log4Net configuration
                AbpBootstrapper.IocManager.IocContainer
                    .AddFacility<LoggingFacility>(f => f.UseLog4Net()
                        .WithConfig("log4net.config")
                    );
    

    and log4net.config is:

    <?xml version="1.0" encoding="utf-8" ?>
    <log4net>
      <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender" >
        <file value="App_Data/Logs/Logs.txt" />
        <encoding value="utf-8" />
        <appendToFile value="true" />
        <rollingStyle value="Size" />
        <maxSizeRollBackups value="10" />
        <maximumFileSize value="10000KB" />
        <staticLogFileName value="true" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%-5level %date [%-5.5thread] %-40.40logger - %message%newline" />
        </layout>
      </appender>
      <root>
        <appender-ref ref="RollingFileAppender" />
        <level value="DEBUG" />
      </root>
    </log4net>
    

    I can add logging in the code like this:

    Logger.Info("test ");
    

    without it throwing errors, but no Log.txt is generated.

    if it wouldnt have rights to write the file, should it throw errors?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Kythor,

    Can you change

    AbpBootstrapper.IocManager.IocContainer
        .AddFacility<LoggingFacility>(f => f.UseLog4Net()
            .WithConfig("log4net.config")
        );
    

    to

    AbpBootstrapper.IocManager.IocContainer
        .AddFacility<LoggingFacility>(f => f.UseLog4Net()
            .WithConfig(Server.MapPath("log4net.config"))
        );
    
  • User Avatar
    0
    kythor created

    I got it to work, thanks for your help!