c# - Log4Net error : “Failed to find configuration section log4net” AdoNetAppender -
i made new project testing. trying use log4net
write db. getting error.i think in config missing.
log4net:error failed find configuration section 'log4net' in application's .config file. check .config file <log4net> , <configsections> elements. configuration section should like: <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler,log4net" />
assemblyinfo.cs
[assembly: log4net.config.xmlconfigurator(configfile = "logging.config", watch = true)]
logging.config
<!--this root of config file--> <configuration> <!-- level 0 --> <!--this specifies section name is--> <configsections> <!-- level 1 --> <section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net"/> <!-- level 2 --> </configsections> <log4net> <!-- level 1 --> <appender name="adonetappender" type="log4net.appender.adonetappender"> <buffersize value="1" /> <connectiontype value="system.data.sqlclient.sqlconnection, system.data, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" /> <connectionstring value="data source=xxx;initial catalog=xxx; integrated security=true;"/> <commandtext value="insert dbo.log ([date],[thread],[level],[logger],[message],[exception]) values (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parametername value="@log_date"/> <dbtype value="datetime"/> <layout type="log4net.layout.rawtimestamplayout"/> </parameter> <parameter> <parametername value="@thread"/> <dbtype value="string"/> <size value="255"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%thread"/> </layout> </parameter> <parameter> <parametername value="@log_level"/> <dbtype value="string"/> <size value="50"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%level"/> </layout> </parameter> <parameter> <parametername value="@logger"/> <dbtype value="string"/> <size value="255"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%logger"/> </layout> </parameter> <parameter> <parametername value="@message"/> <dbtype value="string"/> <size value="4000"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%message"/> </layout> </parameter> <parameter> <parametername value="@exception"/> <dbtype value="string"/> <size value="2000"/> <layout type="log4net.layout.exceptionlayout"/> </parameter> </appender> <root> <level value="all"/> <appender-ref ref="adonetappender"/> </root> <logger> <!-- level 2 --> <level /> <!-- level 3 --> <appender-ref /> <!-- level 3 --> </logger> </log4net> </configuration>
form1.cs
private static readonly log4net.ilog log = log4net.logmanager.getlogger (system.reflection.methodbase.getcurrentmethod().declaringtype); private void simplebutton1_click(object sender, eventargs e) { log4net.config.xmlconfigurator.configure(); log.info("info logging"); log.debug("dadasdasd"); }
your log4net conf should following should not put on top of log4net node since configuration in independent file:
<log4net> <!-- level 1 --> <appender name="adonetappender" type="log4net.appender.adonetappender"> <buffersize value="1" /> <connectiontype value="system.data.sqlclient.sqlconnection, system.data, version=2.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" /> <connectionstring value="data source=xxx;initial catalog=xxx; integrated security=true;"/> <commandtext value="insert dbo.log ([date],[thread],[level],[logger],[message],[exception]) values (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <parameter> <parametername value="@log_date"/> <dbtype value="datetime"/> <layout type="log4net.layout.rawtimestamplayout"/> </parameter> <parameter> <parametername value="@thread"/> <dbtype value="string"/> <size value="255"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%thread"/> </layout> </parameter> <parameter> <parametername value="@log_level"/> <dbtype value="string"/> <size value="50"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%level"/> </layout> </parameter> <parameter> <parametername value="@logger"/> <dbtype value="string"/> <size value="255"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%logger"/> </layout> </parameter> <parameter> <parametername value="@message"/> <dbtype value="string"/> <size value="4000"/> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%message"/> </layout> </parameter> <parameter> <parametername value="@exception"/> <dbtype value="string"/> <size value="2000"/> <layout type="log4net.layout.exceptionlayout"/> </parameter> </appender> <root> <level value="all"/> <appender-ref ref="adonetappender"/> </root> <logger> <!-- level 2 --> <level /> <!-- level 3 --> <appender-ref /> <!-- level 3 --> </logger> </log4net>
if still facing problems try load conf following:
xmlconfigurator.configure(new fileinfo("path\logging.config")
Comments
Post a Comment