如何在Visual Studio 2015年ASP.NET MVC C#log4net的配置 [英] How to configure Log4Net with ASP.NET MVC C# in Visual Studio 2015
问题描述
要帮助我的程序开发和调试,我想软件日志记录到文件中。而不是让我自己的日志系统,我想使用log4net的与ASP.net MVC的,但我遇到了问题,将其设置在Visual Studio 2015年,如:
To help with my program development and debugging, I want to record software logs to files. Rather than make my own log system, I am trying to use Log4Net with ASP.net MVC, but I have run into problems setting it up in Visual Studio 2015, like:
-
如何设置web.config文件/页面的Global.asax?
How to setup web.config / Global.asax page?
如何在2015年VS安装组件?
How to install components in VS 2015?
如何使用它来部署我的 * CS
文件?
How to use it to deploy in my *.cs
file?
哪些步骤在Visual Studio 2015年正确地配置log4net的在ASP.NET MVC C#?
What are the steps to properly configure Log4Net With ASP.NET MVC C# in Visual Studio 2015?
推荐答案
Step1: Please use Nuget to get Log4Net
第二步:添加字符串
log4net.Config.XmlConfigurator.Configure();
in Global.asax.cs
第三步:添加另两节
答:首先栏目
Step3: Adding another two sections A: First section
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
在标签之间的Web.Config &LT; configSections&GT; ...&LT; / configSections&GT;
in Web.Config between tag <configSections>...</configSections>
乙:次节,请插入在&LT以下code;结构&gt; ...&LT; /结构&gt;
<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>
决赛:请插入如下code到要保存日志任何控制器页
Final: Please insert as following code to any controller pages that you want to save log
private static log4net.ILog Log { get; set; }
ILog log = log4net.LogManager.GetLogger(typeof(HomeController));
public ActionResult Index()
{
log.Debug("Debug message");
log.Warn("Warn message");
log.Error("Error message");
log.Fatal("Fatal message");
ViewBag.Title = "Home Page";
return View();
}
这篇关于如何在Visual Studio 2015年ASP.NET MVC C#log4net的配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!