How to use Log4net from Nuget with Visual Studio i

2020-02-15 08:56发布

Hi all I know how to quick and easy way to build the Log4Net in my ASP.NET MVC project [Reference],however how to do it in pure " Web Form " project, please help me to solve this issue, step by step thanks.

By the way I have been looking for the some solutions to deal with my problem too,but it isn't good way to me, like as follow question at the Stackoverflow
1. Using log4net with asp.net web forms
2. unable to write logs to a file using log4net in an ASP.NET Web Forms Site

PS: I will been trying solve this issue too, hopefully you or me can find out some good ideas to handle this.

1条回答
可以哭但决不认输i
2楼-- · 2020-02-15 09:12

4 steps to handle this issue for web form
Step 1:
Using Nuget to get the Log4Net
enter image description here

Step 2:
In Global please put this sentence "log4net.Config.XmlConfigurator.Configure();" in the Application_Start enter image description here

Step 3: In the Web.Config please write down as following code in the web.config

 <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="logs\log.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="100KB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    <root>
      <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

enter image description here

Final step:
Putting as following code in one page where you want to record the log

  private static log4net.ILog Log { get; set; }= log4net.LogManager.GetLogger(typeof(_03_account));

        protected void Page_Load(object sender, EventArgs e)
        {

            Log.Debug("Debug message");
            //log.Warn("Warn message");
            //log.Error("Error message");
            //log.Fatal("Fatal message");
        }

enter image description here

查看更多
登录 后发表回答