如何将log4net配置写入appsettings.json? [英] How to write log4net config into appsettings.json?

查看:736
本文介绍了如何将log4net配置写入appsettings.json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将log4net实施为.NET core 2.0,以登录到文本文件.由于log4net具有一个配置文件,该文件中包含XML configuration.因此,我创建了一个单独的文件log4net.config来设置其配置,并且工作正常.但是我想将其配置设置为appsettings.json.是否可以将log4net配置写入appsettings.json.

I have implemented log4net into .NET core 2.0, to log into a text file. As log4net have a config file, which is having XML configuration in it. So, I have created a separate file log4net.config to set its configuration and it is working fine. But I want to set its configuration into appsettings.json. Is it possible to write the log4net configuration into appsettings.json.

<appSettings>
    <add key="IsLog" value="True" />
    <add key="MaxThreads" value="3" />
  </appSettings>
  <log4net debug="false">
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
 <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
    <file value="./logs/logfile.txt" />
    <appendToFile value="false" />
    <rollingStyle value="Size" />
    <maxSizeRollBackups value="-1" />
    <maximumFileSize value="50GB" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
    </layout>
</appender>   
  </log4net>

这是我的XML配置.

我发现了这个

I have found this question in which it is mentioned that log4net don't support Json projects. Is it possible to write its configuration into appsettings.json.

推荐答案

也许这不是正确的方法,但是无论如何,我设法在appSettings.json中使用了我的log4net.config.我将答案放在这里,这样可以在某人不想为该项目使用额外的配置文件时为其提供帮助.

Maybe it is not the right way but, anyhow I managed to use my log4net.config in appSettings.json. I am putting my answer here so it can help someone if they don't want to use an extra config file for there project.

所以我所做的就像是将我的 XML 转换为 JSON ,然后在appSettings.json上将 JSON 用作字符串一样我使用以下代码读取字符串.

So what I have done is like by converting my XML into JSON and used the JSON as a string on my appSettings.json after that I use the following code to read the string.

appSettings.json

appSettings.json

{
  "ConnectionStrings": {
    "LoggerConfig": "Config string"
  }
}

Json XML 的转换,使用Newtonsoft.Json

Json to XML Conversion using Newtonsoft.Json

 string xmlElement = _configuration["ConnectionStrings:LoggerConfig"];
 XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(xmlElement);
 XmlConfigurator.Configure(logRepository, GenerateStreamFromString(doc.InnerXml));

此代码用于将 JSON 转换为 XML ,但不会将XML内容作为 Element 提供,因此,我将其用作一条小溪. 为了将字符串转换为流,我使用了以下代码.

This code is used to convert the JSON into XML but it will not provide the XML content as Element so, I used it as a stream. For converting the string into a stream I used the following code.

        public static Stream GenerateStreamFromString(string s)
        {
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(s);
            writer.Flush();
            stream.Position = 0;
            return stream;
        }

它工作正常.

在这里,我习惯先将XML to JSON转换为JSON to XML 将其用作log4net config,但是如果有人愿意,请使用XML 直接作为字符串,这样会减少一些代码.

Here I used to convert first my XML to JSON and again JSON to XML to use it as a log4net config, but if anyone wants then use XML directly as a string, So it will reduce some code.

这篇关于如何将log4net配置写入appsettings.json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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