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:

  1. add reference log4net dll
  2. import following namespaces using log4net; using log4net.config;
  3. add following code global declaring

    private static readonly ilog log =   logmanager.getlogger(system.reflection.methodbase.getcurrentmethod().declaringtype); 
  4. add code constructor

    xmlconfigurator.configure(new fileinfo(appdomain.currentdomain.basedirectory + @"\config\log4net.config")); 
  5. 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>

  1. in try...catch(exception ex) add following line

    log.error(ex) 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -