SoFunction
Updated on 2025-03-08

Example of implementation using log4net in MVC

Today I want to make a logging function. I have done it before, but I forgot it. I did it again today. It took dozens of minutes to record it here.

Step 1: Add

Step 2: Configuration The example is as follows: I configured it directly

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
  <section name="log4net" type=".Log4NetConfigurationSectionHandler, log4net" />
 </configSections>
 <log4net>
  <logger name="Student">
   <level value="ALL" />
   <appender-ref ref="rollingFile" />
  </logger>
  <appender name="rollingFile" type=",log4net">
   <File value="log/" />
   <DatePattern value="yyyy-MM-dd".txt"" />
   <StaticLogFileName value="false" />
   <maxSizeRollBackups value="-1" />
   <RollingStyle value="Date" />
   <AppendToFile value="false" />
   <MaximumFileSize value="1024MB" />
   <layout type=",log4net">
    <ConversionPattern value="%-38m %-7p %-20d %n" />
   </layout>
  </appender>
 </log4net>
 
<!--The following is not!!!-->
 <appSettings>
  <add key="webpages:Version" value="3.0.0.0" />
  <add key="webpages:Enabled" value="false" />
  <add key="ClientValidationEnabled" value="true" />
  <add key="UnobtrusiveJavaScriptEnabled" value="true" />
 </appSettings>
 <>
  <compilation debug="true" targetFramework="4.7.2" />
  <httpRuntime targetFramework="4.7.2" />
 </>
 
 
</configuration>

Step 3: Add     (); as follows:

  public class MvcApplication : 
  {
    protected void Application_Start()
    {
      ();
      ();
      ();
      ();
      ();
    }
  }

Step 4: Add Log Help Class and use it. What you need to pay attention to here is the name correspondence. See the comments in my code below.

  public class LogHelper
  {
    //Student is the value of the name of your configuration file <logger name="Student">    private static  log = ("Student");
 
    public static void Debug(object message, Exception e)
    {
      (message, e);
    }
 
    public static void Debug(object message)
    {
      (message);
    }
 
    public static void Info(object message)
    {
      (message);
    }
 
    public static void Warn(object message)
    {
      (message);
    }
 
    public static void Error(object message)
    {
      (message);
    }
 
    public static void Error(object message, Exception e)
    {
      (message, e);
    }
 
    public static void Log(object message)
    {
      (message);
    }
  }

The content of the configuration file is relatively simple. Just take a look at other detailed ones on Baidu.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.