views:

50

answers:

4

hi, i am trying to configure a dynamic name for my log but i can't.

here the code on my web.config file:

<appender name="RollingFile" type="log4net.Appender.RollingPatternFileAppender">
       <file type="log4net.Util.PatternString" value="..l\log\%property{LogName}" />
       <appendToFile value="true" />
       <rollingStyle value="Size" />
       <maxSizeRollBackups value="-1" />
       <maximumFileSize value="5000KB" />
       <staticLogFileName value="true" />
       <countDirection value="1"/>
       <layout type="log4net.Layout.PatternLayout">
           <conversionPattern value="%m%n" />
  </layout>
       <filter type="log4net.Filter.PropertyFilter">
           <Key value="Version" />
           <StringToMatch value="1" />

  </filter>
   <filter type="log4net.Filter.DenyAllFilter" />

     <root>
       <level value="DEBUG"/>
       <appender-ref ref="RollingFile"/>
     </root>

here the global.asax

log4net.Config.XmlConfigurator.Configure();

and here the codebehind:

log=LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
log4net.ThreadContext.Properties["LogName"] = sito + "_Truck_Log_" + DateTime.Today.ToString("dd-MM-yyyy") + ".txt";

it does not creates any file. what is wrong in this configuration?

Thx in advance!

A: 

You have to set the thread context property before you configure log4net.

Stefan Egli
i have moved log4net.Config.XmlConfigurator.Configure(); after to set the thread context property but still at not work anyway.
A: 

i have moved log4net.Config.XmlConfigurator.Configure(); after to set the thread context property but still at not work anyway. Another tip? thx a lot!

A: 

Since you say that it does not create any file, are you sure this is not a permission issue? (Ie, does it create a file in the correct place if you don't try to use a dynamic name?) Also, are you sure the sito variable does not contain invalid characters?

I would try setting the property as earlier as possible as it may be attempting to evaluate the LogName property before it is set. (Ie set it before the GetLogger, Configure or any actual logging).

(I do something similar, but in a Winforms application and I use log4net.GlobalContext.Properties rather than log4net.ThreadContext.Properties, both of which obviously make things easier).

sgmoore
Hi sgmoore, i am
A: 

hi sgmoore, first of all, thx about the answer. I am sure that i don't have permissions problems and i am sure that the "sito" variable does not contains invalid characters.

i am doing something like this:

in codebehind of my page:

log4net.GlobalContext.Properties["LogName"] = sito + "_Truck_Log_" + DateTime.Today.ToString("dd-MM-yyyy")+".txt";
log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

in global.asax, this one:

void Application_Start(object sender, EventArgs e) 
    {

     log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ApplicationBase 
                                                                + "log4net.config")); 
    }

but it still not works ...

By default Log4net is designed to fail silently (ie not display the error message) But if you have physical access to the webserver and run DebugView from Sysinterals( http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx) it should show you any error messages which might give a clue why it is not creating the file.
sgmoore
I should also maybe point out is that I have never used the Filter options and not quite sure how they work. Would it be possible to remove them to see if they are causing the problem?
sgmoore