5

I am using log4net and have completely setup it up with param name="File" value= "C:\Application.log". Yet the file is not created in C:. I am running Windows 7 and maybe something like permissions is preventing the file from being created.

Here is the app.config:

<?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>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

4 Answers4

7

You must provide a real file name. What you defined within your config is a folder name. Instead of:

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

use something like:

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

Also, you'll probably need elevated permissions for your application to write the log to the root c: folder. UAC won't let you write to root folder.

Like Andy said, you'll be better to choose some subfolder of Windows Users folder like:

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

log4net has some predefined variables you can use to target special folders. There are some questions about that here on SO:

How to specify common application data folder for log4net?

C# how to specify the appData file path in the app.config file

Community
  • 1
  • 1
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
1

My problem was the order of sections in my App.config file. I had my <startup> section first, then my <configSections>. For some reason I wasn't getting an error in my Windows app, but it did give an error in a Console app. Apparently <configSections> must be the first section under <configuration>

So, instead of this:

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

Do this:

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
Philipp M
  • 1,877
  • 7
  • 27
  • 38
CTBrewski
  • 313
  • 4
  • 7
1

Yeah, make sure the user that is executing the application has write permissions to c:.

Better yet, you probably don't want to write your application log to the root c:\ directory. It would probably be better to choose a location where your app is installed, or somewhere under Documents and Settings (or the Windows 7 equivalent).

Andy White
  • 86,444
  • 48
  • 176
  • 211
  • Also make sure that the appender is specified on the logger. It's easy to miss. If you are running the application in the debug mode under VS you can use a trace appender and the logging output will show up in the Output window, very useful if Log4Net itself says something. – Skurmedel Jan 07 '10 at 23:49
0

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

I moved the appSettings entry with my log4net config entries above the startup 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>
Community
  • 1
  • 1
jamesmortensen
  • 33,636
  • 11
  • 99
  • 120