Initialize log4net settings from database

不羁的心 提交于 2019-12-13 04:21:10

问题


Currently my application (C# Console App) uses file to initialize log4net, is there a way to initialize the log4net configurations from the database ?

I can either save my current file as XML in DB (SQL) but I am not sure how to initialize log4net from that within my application.

EDIT :

Current Implementation:

 _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            var file = new FileInfo(System.Configuration.ConfigurationManager.AppSettings["log4net.Config"]);
            log4net.Config.XmlConfigurator.Configure(file);

AppSettings has a location of the physical file which has the following:

<log4net>
    <appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
      <file type="log4net.Util.PatternString" value="c:\%property{LogName}" />
      <appendToFile value="true" />
      <rollingStyle value="Date" />
      <staticLogFileName value="false" />
      <datePattern value="MM-dd-yyyy'.log'" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level – %message%newline" />
      </layout>
    </appender>

    <root>
      <appender-ref ref="FileAppender"/>

    </root>
  </log4net>

回答1:


There's an overload to XmlConfigurator.Configure that takes a stream as the parameter - so you just read your config from the database, turn it into a stream, and pass it to that method:

string config = GetConfigFromDb();
using (var MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(config))
{ 
     log4net.Config.XmlConfigurator.Configure(ms);
}


来源:https://stackoverflow.com/questions/17236965/initialize-log4net-settings-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!