L A

L A

  • NA
  • 170
  • 171.3k

Logging to different types - log4net

Jul 9 2018 1:35 AM
Hi, I'm using Log4net in an ASP.Net MVC for logging to different output types (file/ console & DB).
 
Added new log4net.config to project
  1. <log4net>    
  2.   <root>    
  3.     <level value="ALL" />    
  4.     <appender-ref ref="file" />    
  5.     <appender-ref ref="DBLog" />    
  6.   </root>    
  7.   <appender name="file" type="log4net.Appender.RollingFileAppender">    
  8.     <file value="localDirPath\myapp.log" />    
  9.     <appendToFile value="true" />    
  10.     <rollingStyle value="Size" />    
  11.     <maxSizeRollBackups value="5" />    
  12.     <maximumFileSize value="5MB" />    
  13.     <staticLogFileName value="true" />    
  14.     <layout type="log4net.Layout.PatternLayout">    
  15.       <conversionPattern value="%-7p%d{yyyy-MM-dd HH:mm:ss tt} –%X{user}– %m method:%method %n stacktrace:%stacktrace{5} %n %logger %n type:%type %n line: %line %n" />    
  16.     </layout>    
  17.   </appender>    
  18.     
  19.   <appender name="DBLog" type="log4net.Appender.AdoNetAppender">    
  20.     <bufferSize value="1" />    
  21.     <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />    
  22.     <connectionString value="Server=serverName;Database=DemoDB;integrated security=True;persist security info=True;" />    
  23.     <commandText value="INSERT INTO dbo.Log4net_Log ([Date],[Level],[Logger],[User],[Message],[Exception])    
  24.  VALUES (@log_date, @log_level, @logger, @user, @message, @exception)" />    
  25.     <parameter>    
  26.       <parameterName value="@log_date" />    
  27.       <dbType value="DateTime" />    
  28.       <layout type="log4net.Layout.RawTimeStampLayout" />    
  29.     </parameter>    
  30.     <parameter>    
  31.       <parameterName value="@log_level" />    
  32.       <dbType value="String" />    
  33.       <size value="50" />    
  34.       <layout type="log4net.Layout.PatternLayout">    
  35.         <conversionPattern value="%p" />    
  36.       </layout>    
  37.     </parameter>    
  38.     <parameter>    
  39.       <parameterName value="@logger" />    
  40.       <dbType value="String" />    
  41.       <size value="255" />    
  42.       <layout type="log4net.Layout.PatternLayout">    
  43.         <conversionPattern value="%c" />    
  44.       </layout>    
  45.     </parameter>    
  46.     <parameter>    
  47.       <parameterName value="@user" />    
  48.       <dbType value="String" />    
  49.       <size value="50" />    
  50.       <layout type="log4net.Layout.PatternLayout">    
  51.         <conversionPattern value="%X{user}" />    
  52.       </layout>    
  53.     </parameter>    
  54.     <parameter>    
  55.       <parameterName value="@message" />    
  56.       <dbType value="String" />    
  57.       <size value="4000" />    
  58.       <layout type="log4net.Layout.PatternLayout">    
  59.         <conversionPattern value="%m" />    
  60.       </layout>    
  61.     </parameter>    
  62.     <parameter>    
  63.       <parameterName value="@exception" />    
  64.       <dbType value="String" />    
  65.       <size value="2000" />    
  66.       <layout type="log4net.Layout.ExceptionLayout" />    
  67.     </parameter>    
  68.   </appender>    
  69. </log4net>    
In line # 22 i'm using connection stringof my DB to which logs would be written, but i'm already using same connection string in 'web.config' to fetch data and populate on UI.
  • How to use connection string specified in web.config for logging data to table using log4net.
  • As i'm logging different parameters to file such as time, userName, method and stacktrace; All log levels (Info, warn, error) log complete parameters, which i'm not intresered in. How to log only time, userName, method under INFO level & time, userName, method, stacktrace under WARN/ Error level
 
Added a new log4netHelper.cs to project
  1. public class log4netHelper  
  2. {    
  3.     private static readonly ILog log4Net = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);    
  4.     public log4netHelper()    
  5.     {    
  6.         if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated)    
  7.             MDC.Set("user", HttpContext.Current.User.Identity.Name);    
  8.     }    
  9.     public static void LogMessage(string message)    
  10.     {    
  11.         log4Net.Info(message);    
  12.     }    
  13.     
  14.     public static void LogError(string message, Exception ex)    
  15.     {    
  16.         log4Net.Error(message, ex);    
  17.     }    
  18. }   
 
  • How to get current caller method which is calling log4net.Message()? (MSDN says to use 'System.Runtime.CompilerServices.CallerMemberName' attribute, but how to incorporate this into my logic.
  • Current log4net.config have both RollingFileAppender & AdoNetAppender but only RollingFileAppender is logging data to file, nothing is logged to Database table.

Answers (2)