如何通过ASP.NET Web窗体中的Visual Studio使用Nuget中的Log4net(简便方法) [英] How to use Log4net from Nuget with Visual Studio in the ASP.NET Web Form (Easy method)

查看:232
本文介绍了如何通过ASP.NET Web窗体中的Visual Studio使用Nuget中的Log4net(简便方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我知道如何在我的ASP.NET MVC项目[

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.

顺便说一句,我也一直在寻找一些解决我的问题的解决方案,但这对我来说不是一个好方法,例如在Stackoverflow上关注以下问题
1.将log4net与asp.net Web表单一起使用
2.无法在ASP.NET Web窗体站点

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:我也将尝试解决此问题,希望您能找到一些解决此问题的好方法.

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

推荐答案

4个步骤来处理Web表单的此问题
步骤1:
使用Nuget获取Log4Net

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

步骤2:
在全局中,请将此句子"log4net.Config.XmlConfigurator.Configure();"放在Application_Start中

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

第3步: 在Web.Config中,请在web.config

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>

最后一步:
将以下代码放在要记录日志的一页中

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");
        }

这篇关于如何通过ASP.NET Web窗体中的Visual Studio使用Nuget中的Log4net(简便方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆