Log file not created?

I use log4net and fully configure it with the parameter name = "File" value = "C: \ Application.log". However, the file was not created in C :. I am running Windows 7 and perhaps something like permissions is preventing the creation of the file.

Here is the app.config application:

<?xml version="1.0" encoding="utf-8" ?>  
<configuration>  
  <configSections>  
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />  
  </configSections>`  

  <log4net>  
    <root>  
      <level value="DEBUG" />  
      <appender-ref ref="LogFileAppender" />  
    </root>  
    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >  
      <param name="File" value="C:\Users\Mohit\Documents\Application.log" />  
      <param name="AppendToFile" value="true" />  
      <rollingStyle value="Size" />  
      <maxSizeRollBackups value="10" />  
      <maximumFileSize value="10MB" />  
      <staticLogFileName value="true" />  
      <layout type="log4net.Layout.PatternLayout">  
        <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />  
      </layout>  
    </appender>  
  </log4net>  
</configuration>
+5
source share
4 answers

You must provide a real file name. What you defined in your configuration is the name of the folder. Instead:

<param name="File" value="C:\Users\Mohit\Documents" />

use something like:

<param name="File" value="C:\Users\Mohit\Documents\log.txt" />

In addition, you will probably need elevated permissions for your application to write the log to the root folder c :. UAC will not allow you to write to the root folder.

, - Windows Users, :

c:\Users\Mohit\AppData\Local\<MyApplication>

log4net , . , SO:

log4net?

# appData app.config

+7

, , , , c:.

, , , c: \. , , , - ( Windows 7).

+1

App.config. <startup>, <configSections>. - Windows, . -, <configSections> <configuration>

, :

<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
+1
source

What solved my problem was basically what CTBrewski posted here (+1 by the way!) , But my App.config had an appSettings entry, not a configSections entry.

I moved the appSettings entry with my log4net configuration entries over the start entry, and the logs were then written to the user profile:

<configuration>
  <appSettings>
    <add key="log4net.Config" value="log4net.config" />
    <add key="log4net.Config.Watch" value="True" />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </appSettings> 
  <startup>
    <supportedRuntime version="v2.0.50727" />
  </startup>
  ...
  ...

And then, of course, my appender looks like this:

  <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
    <file value="${LOCALAPPDATA}/Synclio/Logs/SynclioWin.log" />
    <appendToFile value="true" />
    <maximumFileSize value="5000KB" />
    <maxSizeRollBackups value="2" />

    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%level %thread %logger - %message%newline" />
    </layout>
  </appender>
0
source

All Articles