I have a framework that uses log4net for logging, I know that we can disable or enable the desired logging in log4net via the config file. I want to develop some settings class so that the admin or user can set the logging etc.. What is the class etc in log4net to get the config file and change the settings in the config file via code? (are there any or should I use .net System.Xml) Any code snippets?
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<root>
<level value="ALL" />
<!--<appender-ref ref="LogFileAppender" />-->
<!--<appender-ref ref="ConsoleAppender" />-->
<appender-ref ref="DEBUG-RollingLogFileAppender" />
<appender-ref ref="Error-RollingLogFileAppender" />
</root>
<appender name="LogFileAppender" type="log4net.Appender.FileAppender" >
<param name="File" value="log-file.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n"/>
<param name="Footer" value="[Footer]\r\n"/>
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />
</layout>
</appender>
<appender name="DEBUG-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="File" value="..\\logs\\debug" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<!--<param name="ConversionPattern" value="%d [%t] %-5p %c %ndc %P{auth} - %m%n" />-->
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
<filter type="log4net.Filter.LevelRangeFilter">
<!--<levelMin value="INFO" />-->
<levelMax value="INFO" />
</filter>
</appender>
<appender name="Error-RollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
<param name="Threshold" value="WARN"/>
<param name="File" value="..\\logs\\errors" />
<param name="AppendToFile" value="true" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Composite" />
<param name="DatePattern" value=".yyyyMMdd'.log'" />
<param name="MaxSizeRollBackups" value="30" />
<param name="MaximumFileSize" value="5MB" />
<layout type="log4net.Layout.PatternLayout,log4net">
<header value=" " />
<footer value=" ======================================================================" />
<param name="ConversionPattern" value="%d [%t] %-5p - %m%n" />
</layout>
</appender>
</log4net>
Your question isn't exactly clear: are you trying to change the currently-running configuration programmatically, or change the config file programmatically?
If it's the latter, I don't think you need anything from log4net itself - and I wouldn't particularly expect it to provide an API to manipulate the file. Instead, I'd use LINQ to XML (assuming .NET 3.5 or higher) and manipulate it myself directly. It's pretty simple, by the looks of it. Study the configuration file format, work out what changes you'd want to make to it, then write the appropriate code for it. You shouldn't even need to reference log4net for this.
it seems you can use Peter's answer and add or remove appender
Create all the appenders in the web config and after the line:
log4net.Config.XmlConfigurator.Configure();
Iterate:
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
foreach (var appender in root.Appenders)
Console.WriteLine(appender.Name);
Remove:
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
IAppender removedAppender = null;
if (root.Appenders.ToArray().Where(x => x.Name == "GelfUdpAppender").Any())
removedAppender = root.RemoveAppender("GelfUdpAppender");
Add:
FileAppender appender = new FileAppender();
appender.Name = "RollingFileAppender";
appender.File = "Log.log";
appender.AppendToFile = true;
PatternLayout layout = new PatternLayout();
layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
layout.ActivateOptions();
appender.Layout = layout;
appender.ActivateOptions();
var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository()).Root;
root.AddAppender(appender);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With