c# - Logging SQL exceptions from database using log4net -
i have wcf service calls stored procedure in sql database. use log4net logging purpose.
if sql exception thrown database, can sql exception properties procedure name, error line, severity etc., exception object in service.
but there way directly log properties in file using log4net?.
follow these steps:
- add reference log4net dll
- import following namespaces using log4net; using log4net.config;
add following code global declaring
private static readonly ilog log = logmanager.getlogger(system.reflection.methodbase.getcurrentmethod().declaringtype);
add code constructor
xmlconfigurator.configure(new fileinfo(appdomain.currentdomain.basedirectory + @"\config\log4net.config"));
copy following config file under configuration tag
<configsections> section name="log4net" type="log4net.config.log4netconfigurationsectionhandler, log4net" /> </configsections> <log4net debug="true"> <appender name="rollinglogfileappender" type="log4net.appender.rollingfileappender"> <file value="c:\\temp\\errlog.txt" /> <appendtofile value="true" /> <rollingstyle value="size" /> <maxsizerollbackups value="10" /> <maximumfilesize value="10mb" /> <staticlogfilename value="true" /> <layout type="log4net.layout.patternlayout"> <conversionpattern value="%-5p %d %5rms %-22.22c{1} %-18.18m - %m%n" /> </layout> </appender> <root> <level value="debug" /> <level value= "error" /> <appender-ref ref="rollinglogfileappender" /> </root> </log4net>
in try...catch(exception ex) add following line
log.error(ex)
Comments
Post a Comment