如何在Visual Studio 2012上使用ASP.NET(包含MVC)C#配置或设置Log4Net [英] How to configure or setup Log4Net with ASP.NET ( included MVC) C# on Visual Studio 2012 ~ ~
问题描述
我想将软件过程日志记录到文件中.我没有使用自己的日志系统,而是尝试将Log4Net与ASP.NET MVC一起使用,但是遇到了在Visual Studio 2015中对其进行设置的问题,例如:
I want to record software process 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?
如何在VS 2012中安装组件〜?
How to install components in VS 2012 ~?
如何在我的*.cs
文件中使用它?
How to use it in my *.cs
file?
在Visual Studio 2015中使用ASP.NET MVC C#正确配置Log4Net的步骤是什么?
What are the steps to properly configure Log4Net With ASP.NET MVC C# in Visual Studio 2015?
我还写了一个问答集以将其设置为ASP.NET WebForms, 请参阅如何在ASP.NET Web窗体中通过Visual Studio平台使用Nuget的Log4net(简便方法).
I also wrote a Q&A to set it up for ASP.NET WebForms, see How to use Log4net from Nuget with Visual Studio platform in the ASP.NET Web Form (Easy method).
推荐答案
第一步:使用Nuget获取log4net软件包:
Step1: To use the Nuget to get the log4net package:
Step2:通过在Application_Start()
下的Global.asax.cs
文件中添加此调用,告诉log4net从XML配置(Web.config)初始化自身:
Step2: tell log4net to initialize itself from the XML configuration (Web.config), by adding this call in the Global.asax.cs
file under Application_Start()
:
log4net.Config.XmlConfigurator.Configure();
步骤3:在Web.config中的标记<configSections>...</configSections>
之间添加配置部分:
Step3: add the configuration section in Web.config between tag <configSections>...</configSections>
:
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
Step4:插入实际的log4net配置<log4net>...</log4net>
(在<configuration>...</configuration>
内,但在</configSections>
标记之后),请参见
Step4: Insert the actual log4net configuration <log4net>...</log4net>
(Within <configuration>...</configuration>
but after the </configSections>
tag) , see Apache log4net™ Config Examples for more examples:
<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>
现在您可以调用ILog
将实际的日志语句写入配置的附加程序:
Now you're ready to make calls to an ILog
to write actual log statements to the configured appender(s):
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 2012上使用ASP.NET(包含MVC)C#配置或设置Log4Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!