Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write log4net config into 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>

this is my XML configuration.

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.

like image 999
DevProf Avatar asked Aug 29 '18 06:08

DevProf


People also ask

Where do I put log4net in web config?

Add log4net in config file config and enter the following details. Add a class Log. cs in the Utilities folder. Now, in the constructor of this class, instantiate logs for monitoring and debugger loggers.

How do I add Appsettings to json?

Add Json File After adding the file, right click on appsettings. json and select properties. Then set “Copy to Ouptut Directory” option to Copy Always. Add few settings to json file, so that you can verify that those settings are loaded.


1 Answers

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.

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

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

Json to XML Conversion using Newtonsoft.Json

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

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;
        }

and it works fine.

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.

like image 75
DevProf Avatar answered Sep 17 '22 15:09

DevProf