c# - Can you configure log4net in code instead of using a config file? -
i understand why log4net uses app.config
files setting logging - can change how information logged without needing recompile code. in case not want pack app.config
file executable. , have no desire modify logging setup.
is there way me set logging in code rather using app.config
?
here simple config file:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configsections> <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" /> </configsections> <log4net> <appender name="rollinglogfileappender" type="log4net.appender.rollingfileappender"> <file value="logs\eventlog.txt" /> <appendtofile value="false" /> <rollingstyle value="size" /> <maxsizerollbackups value="5" /> <maximumfilesize value="1gb" /> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%date [%thread] %-5level %logger - %message%newline" /> </layout> </appender> <appender name="memoryappender" type="log4net.appender.memoryappender"> </appender> <root> <level value="info" /> <appender-ref ref="rollinglogfileappender" /> <appender-ref ref="memoryappender" /> </root> </log4net> </configuration>
edit:
to clear: goal have no xml file. not embedded resource turn stream. goal define logger programmatically. curious if it's possible , if might find example of syntax.
final solution:1
for may stumble upon in future, here did. made static class below:
using log4net; using log4net.repository.hierarchy; using log4net.core; using log4net.appender; using log4net.layout; namespace spectrum.logging { public class logger { public static void setup() { hierarchy hierarchy = (hierarchy)logmanager.getrepository(); patternlayout patternlayout = new patternlayout(); patternlayout.conversionpattern = "%date [%thread] %-5level %logger - %message%newline"; patternlayout.activateoptions(); rollingfileappender roller = new rollingfileappender(); roller.appendtofile = false; roller.file = @"logs\eventlog.txt"; roller.layout = patternlayout; roller.maxsizerollbackups = 5; roller.maximumfilesize = "1gb"; roller.rollingstyle = rollingfileappender.rollingmode.size; roller.staticlogfilename = true; roller.activateoptions(); hierarchy.root.addappender(roller); memoryappender memory = new memoryappender(); memory.activateoptions(); hierarchy.root.addappender(memory); hierarchy.root.level = level.info; hierarchy.configured = true; } } }
and had replace code called xml file following call:
//xmlconfigurator.configure(new fileinfo("app.config")); // not needed anymore logger.setup();
1(this answer edited question op, took liberty make community answer, see here why)
Comments
Post a Comment