如何在ASP.NET 5.0中为log4net(或其他任何第三方库)添加配置 [英] how to add configuration for log4net (or any other 3rd party library) in ASP.NET 5.0

查看:307
本文介绍了如何在ASP.NET 5.0中为log4net(或其他任何第三方库)添加配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 ASP.NET 5.0功能上的Scott Gu的博客.博客中提到的一项新功能是使用json文件作为配置和消除Web.config文件.

I was reading Scott Gu's blog on ASP.NET 5.0 features and one of the new feature mentioned in the blog is to use json file as configuration and elimination of Web.config file.

关于该功能,我有几个问题.

I have few questions around that feature.

假定我具有以下log4net配置,该配置先前已在ASP.NET的早期版本中添加到Web.Config中.

Assume I have following log4net configuration which was previously added to Web.Config in previous version of ASP.NET

配置文件

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />

<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <file value="C:\\TestProj\\TestLog.txt" />
  <appendToFile value="true" />
  <rollingStyle value="Size" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <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>

一个人如何在config.json中添加节?

How would one add sections in config.json ?

一个人如何转换以上xml并将其添加到config.json中?

How would one convert above xml and add it to config.json?

第3方库(在我的示例中为log4net)或库的用户是否必须添加某种类型的自定义转换api以支持基于json的配置,以便利用ASP.NET 5.0中提供的新配置功能? /p>

Does 3rd Party library ( In my example log4net ) or users of the library have to add some type of custom conversion api to support json based configuration, in order to take advantage of new configuration feature provided in ASP.NET 5.0?

推荐答案

最近,我在log4net上遇到了同样的问题.我设法做到了,如下所示:

Recently I had the same problem with log4net. I managed to do it working as following:

创建包含log4net的配置部分的log4net.xml文件

Create log4net.xml file containing the configuration section for log4net

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
...
</log4net>

您可以将文件放在项目根文件夹中.

You can put the file in the project root folder.

Startup.Startup()方法中,您可以配置提供XML作为配置的log4net:

And in the Startup.Startup() method you can configure the log4net providing the xml as a configuration:

public Startup(IApplicationEnvironment appEnv)
{
    // ... 

    XmlConfigurator.Configure(new FileInfo(Path.Combine(appEnv.ApplicationBasePath, "log4net.xml")));
}

我希望这会有所帮助.

这篇关于如何在ASP.NET 5.0中为log4net(或其他任何第三方库)添加配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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