问题
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