Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log file not created?

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>
like image 598
Mohit Deshpande Avatar asked Jan 07 '10 23:01

Mohit Deshpande


3 Answers

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

like image 170
Miroslav Popovic Avatar answered Oct 16 '22 10:10

Miroslav Popovic


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).

like image 39
Andy White Avatar answered Oct 16 '22 11:10

Andy White


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>
like image 1
CTBrewski Avatar answered Oct 16 '22 11:10

CTBrewski