如何在 ASP.NET Web 窗体中将来自 Nuget 的 Log4net 与

How to use Log4net from Nuget with Visual Studio in the ASP.NET Web Form (Easy method)(如何在 ASP.NET Web 窗体中将来自 Nuget 的 Log4net 与 Visual Studio 一起使用(简单方法))
本文介绍了如何在 ASP.NET Web 窗体中将来自 Nuget 的 Log4net 与 Visual Studio 一起使用(简单方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我知道如何在我的 ASP.NET MVC 项目中快速简便地构建 Log4Net [

第 2 步:
在全局请把这句话log4net.Config.XmlConfigurator.Configure();"放在Application_Start

第 3 步:在 Web.Config 中,请在 web.config 中写下如下代码

 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/></configSections><log4net debug="true"><appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"><file value="logslog.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"/></布局></appender><根><level value="DEBUG"/><appender-ref ref="RollingLogFileAppender"/></root>

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

 private static log4net.ILog Log { get;放;}= log4net.LogManager.GetLogger(typeof(_03_account));protected void Page_Load(object sender, EventArgs e){Log.Debug("调试信息");//log.Warn("警告信息");//log.Error("错误信息");//log.Fatal("致命信息");}

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.

解决方案

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

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

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="logslog.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 窗体中将来自 Nuget 的 Log4net 与 Visual Studio 一起使用(简单方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)