Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure log4net for asp.net MVC3 project

Ok, so I'm understood how to configure the log4Net in my application, But now

First I want to improve the configuration by differencing the level of the logs if the application it's a release or a debug, how can I do this?.

Second, If I had a folder in my project called LOG how can I set the configuration, to not used the physical folder of my application??

for example Instead of:

<file value="C:\physicalpath\LOG\Log.log" /> 

used

<file value="\LOG\Log.log" /> 

or

<file value="%some_variable%\LOG\Log.log" /> 
like image 993
Jorge Avatar asked Jan 12 '12 16:01

Jorge


People also ask

How logger is implemented in MVC?

Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers. Here, you need to create Debug(), Error(), Info(), Warn(), Fatal() methods which will call respective methods from Log4 net.


1 Answers

The documenation is straight forward:

file: the full or relative path to the log file.

So all you need to have is the full path like C:\physicalpath\LOG\Log.log or the ralative one, this needs to start with the dot char . like .\App_Data\Log4Net.Logs

you can also use the folder name in the file attribute, then you must use the datePattern attribute to specify the file name, for example:

<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
  <file value=".\\App_Data\\Log4Net.Logs\\backend"/>
  <datePattern value=".yyyy-MM-dd'.log'"/>
  <appendToFile value="true"/>
  <maximumFileSize value="256KB"/>
  <maxSizeRollBackups value="2"/>
  <rollingStyle value="Date"/>
  <staticLogFileName value="false"/>
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
  <layout type="log4net.Layout.PatternLayout">
     <conversionPattern value="%level %thread %logger - %message%newline"/>
  </layout>
</appender>

Also remember to add the

<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>

So you can avoid that log4net lock the file and you can't used it to append your messages.

If you're not used to log4net, don't forget to add the <root> node, this is the the one that let's log4net know what you want to use and not the <appender> nodes, for example, you can have 10 <appender> nodes and use only one, the <root> node is then only configured with the one you want to use...


here is a full configuration with 2 Mongo Appenders and 1 File Appender, the <root> specifies that only the file appender is in use:

  <log4net>
    <appender name="MongoAppender" type="log4net.Appender.MongoDBAppender, log4mongo-net">
      <!-- MongoDB 1 connection options -->
      <host value="staff.mongohq.com"/>
      <port value="10077"/>
      <databaseName value="myApp_2011"/>
      <collectionName value="logs_net"/>
      <userName value="myself"/>
      <password value="123456"/>
    </appender>
    <appender name="MongoAppenderAppHarbor" type="log4net.Appender.MongoDBAppender, log4mongo-net">
      <!-- MongoDB 2 connection options -->
      <host value="staff.mongohq.com"/>
      <port value="10048"/>
      <databaseName value="d1741d63-46b1-4a44-9c49-8c28cecae36b"/>
      <collectionName value="logs_net"/>
      <userName value="myself"/>
      <password value="123456"/>
    </appender>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <!-- Local file options -->
      <file value=".\\App_Data\\Log4Net.Logs\\backend"/>
      <datePattern value=".yyyy-MM-dd'.log'"/>
      <appendToFile value="true"/>
      <maximumFileSize value="256KB"/>
      <maxSizeRollBackups value="2"/>
      <rollingStyle value="Date"/>
      <staticLogFileName value="false"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %thread %logger - %message%newline"/>
      </layout>
    </appender>
    <root>
      <!--
         <level value="DEBUG" />
         <appender-ref ref="MongoAppender" />
         <appender-ref ref="MongoAppenderAppHarbor" />
      -->
      <appender-ref ref="FileAppender"/>
    </root>
  </log4net>
like image 111
balexandre Avatar answered Oct 23 '22 02:10

balexandre