c# - We need enterprise library logging to throw errors -


do know how can errors enterprise library 5 logging block?

as know, logging's philosophy non disrupting, if there error in configuration example, not error thrown.

but in our application, logging critical, need have application out of order, running without logs.

so need configure so, throw error if there any.

i've found events here: enterprise library logging application block options

so should able listen event, , throw our own error @ least, not able reference logging instrumentation, because, cannot see getinstrumentationeventprovider() method in following code.

logginginstrumentationprovider instrumentation = logger.writer.getinstrumentationeventprovider() logginginstrumentationprovider;  instrumentation.failureloggingerror += (s, z) => { throw z.exception; }; 

thank help.

i think logginginstrumentationprovider definition changed between version 4 , 5 remove events posted approach won't work enterprise library 5.

there few ways change behavior enterprise library not swallow exceptions:

  • modify enterprise library source code implement behavior want
  • duplicate logwriterimpl/logsource code , inject container (this approach can seen in enterprise library logging extensions – part 1
  • replace current logginginstrumentationprovider implementation 1 throws

i'm going demonstrate last approach (not because it's best because it's interesting). approach work need create custom logginginstrumentationprovider custom tracelistener throws , configure logging application block errors special source use new tracelistener.

basically, custom tracelistener used error special source , invoked when error occurs trying log standard tracelisteners (or other error typical error scenario). custom tracelistener throw cause logginginstrumentationprovider.firefailureloggingerrorevent method invoked throw causing exception bubble up. (yes, it's long way go!)

first create custom logginginstrumentationprovider:

[hasinstallableresourcesattribute] [performancecountersdefinition(countercategoryname, "loggingcountershelpresource")] [eventlogdefinition("application", "enterprise library logging")] public class mylogginginstrumentationprovider : instrumentationlistener, ilogginginstrumentationprovider {     static enterpriselibraryperformancecounterfactory factory = new enterpriselibraryperformancecounterfactory();     private const string totalloggingeventsraised = "total logging events raised";     private const string totaltracelistenerentrieswritten = "total trace listener entries written";      [performancecounter("logging events raised/sec", "loggingeventraisedhelpresource", performancecountertype.rateofcountspersecond32)]     private enterpriselibraryperformancecounter logeventraised;      [performancecounter(totalloggingeventsraised, "totalloggingeventsraisedhelpresource", performancecountertype.numberofitems32)]     private enterpriselibraryperformancecounter totalloggingeventsraised;      [performancecounter("trace listener entries written/sec", "tracelistenerentrywrittenhelpresource", performancecountertype.rateofcountspersecond32)]     private enterpriselibraryperformancecounter tracelistenerentrywritten;      [performancecounter(totaltracelistenerentrieswritten, "totaltracelistenerentrieswrittenhelpresource", performancecountertype.numberofitems32)]     private enterpriselibraryperformancecounter totaltracelistenerentrieswritten;      private const string countercategoryname = "enterprise library logging counters";     private ieventlogentryformatter eventlogentryformatter;      /// <summary>     /// initializes new instance of <see cref="logginginstrumentationprovider"/> class.     /// </summary>     /// <param name="performancecountersenabled"><code>true</code> if performance counters should updated.</param>     /// <param name="eventloggingenabled"><code>true</code> if event log entries should written.</param>     /// <param name="applicationinstancename">the application instance name.</param>     public mylogginginstrumentationprovider(bool performancecountersenabled,                                           bool eventloggingenabled,                                           string applicationinstancename)         : base(performancecountersenabled, eventloggingenabled, new appdomainnameformatter(applicationinstancename))     {         this.eventlogentryformatter = new eventlogentryformatter("enterprise library logging application block");//resources.blockname);     }      /// <summary>     /// initializes new instance of <see cref="logginginstrumentationprovider"/> class.     /// </summary>     /// <param name="instancename">the instance name.</param>     /// <param name="performancecountersenabled"><code>true</code> if performance counters should updated.</param>     /// <param name="eventloggingenabled"><code>true</code> if event log entries should written.</param>     /// <param name="applicationinstancename">the application instance name.</param>     public mylogginginstrumentationprovider(string instancename,                                           bool performancecountersenabled,                                           bool eventloggingenabled,                                           string applicationinstancename)         : base(instancename, performancecountersenabled, eventloggingenabled, new appdomainnameformatter(applicationinstancename))     {         this.eventlogentryformatter = new eventlogentryformatter("enterprise library logging application block");//resources.blockname);     }      /// <summary>     /// fires <see cref="logginginstrumentationprovider.tracelistenerentrywritten"/> event.     /// </summary>     public void firetracelistenerentrywrittenevent()     {         if (performancecountersenabled)         {             tracelistenerentrywritten.increment();             totaltracelistenerentrieswritten.increment();         }     }      ///<summary>     ///     ///</summary>     /// <param name="exception">the exception describes reconfiguration error.</param>     public void firereconfigurationerrorevent(exception exception)     {         if (exception == null) throw new argumentnullexception("exception");         if (eventloggingenabled)         {             string entrytext = eventlogentryformatter.getentrytext("an unknown error occurred reconfiguring logging application block. reconfiguration not take place."             //resources.reconfigurationfailure             , exception);             eventlog.writeentry(geteventsourcename(), entrytext, eventlogentrytype.error);         }     }      /// <summary>     /// </summary>     /// <param name="message">a message describing failure.</param>     /// <param name="exception">the exception caused failure..</param>     public void firefailureloggingerrorevent(string message, exception exception)     {         if (exception == null) throw new argumentnullexception("exception");         if (eventloggingenabled)         {             string entrytext = eventlogentryformatter.getentrytext(message, exception);              eventlog.writeentry(geteventsourcename(), entrytext, eventlogentrytype.error);         }          // new code throw exception         throw exception;     }      /// <summary>     /// </summary>     /// <param name="configurationexception">the exception describes configuration error.</param>     public void fireconfigurationfailureevent(exception configurationexception)     {         if (configurationexception == null) throw new argumentnullexception("configurationexception");         if (eventloggingenabled)         {             string entrytext = eventlogentryformatter.getentrytext("the error occurred while refreshing logging configuration. configuration not updated."             //resources.configurationfailureupdating             , configurationexception);             eventlog.writeentry(geteventsourcename(), entrytext, eventlogentrytype.error);         }     }      /// <summary>     /// fires <see cref="logginginstrumentationprovider.logeventraised"/> event.     /// </summary>     public void firelogeventraised()     {         if (performancecountersenabled)         {             logeventraised.increment();             totalloggingeventsraised.increment();         }     }      /// <summary/>     /// <param name="message"></param>     public void firelockacquisitionerror(string message)     {         if (eventloggingenabled)         {             string entrytext = eventlogentryformatter.getentrytext(message);             eventlog.writeentry(geteventsourcename(), entrytext, eventlogentrytype.error);         }     }      /// <summary>     /// creates performance counters instrument logging events instance names.     /// </summary>     /// <param name="instancenames">the instance names performance counters.</param>     protected override void createperformancecounters(string[] instancenames)     {         logeventraised = factory.createcounter(countercategoryname, "logging events raised/sec", instancenames);         tracelistenerentrywritten = factory.createcounter(countercategoryname, "trace listener entries written/sec", instancenames);         totalloggingeventsraised = factory.createcounter(countercategoryname, totalloggingeventsraised, instancenames);         totaltracelistenerentrieswritten = factory.createcounter(countercategoryname, totaltracelistenerentrieswritten, instancenames);     } } 

the functional change enterprise library version addition of throw exception in firefailureloggingerrorevent method.

next need create tracelistener throw exception:

[configurationelementtype(typeof(customtracelistenerdata))] public class exceptionthrowingtracelistener : customtracelistener {     private void throw()     {         throw new exception("an error occurred logging.  check error logs.");     }      public override void tracedata(traceeventcache eventcache, string source, traceeventtype eventtype, int id, object data)     {         throw();     }      public override void write(string message)     {         throw();     }      public override void writeline(string message)     {         throw();     } } 

next need configure enterprise library @ startup use our new logginginstrumentationprovider (i'm accessing underlying unitycontainer directly):

iunitycontainer container = new unitycontainer();  container.addnewextension<enterpriselibrarycoreextension>();  var instrumentationsection =     configurationmanager.getsection(instrumentationconfigurationsection.sectionname)     instrumentationconfigurationsection;  ilogginginstrumentationprovider provider = null;  if (instrumentationsection != null) {     provider = new mylogginginstrumentationprovider(         instrumentationsection.performancecountersenabled,         instrumentationsection.eventloggingenabled,         instrumentationsection.applicationinstancename); } else {     provider = new mylogginginstrumentationprovider(false, false, "defaultapplication"); }  container.registerinstance<ilogginginstrumentationprovider>(provider);  enterpriselibrarycontainer.current = new unityservicelocator(container); 

then can configure errors special source log our failure , throw exception:

<?xml version="1.0"?> <configuration>   <configsections>     <section name="instrumentationconfiguration" type="microsoft.practices.enterpriselibrary.common.instrumentation.configuration.instrumentationconfigurationsection, microsoft.practices.enterpriselibrary.common, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="true" />     <section name="loggingconfiguration" type="microsoft.practices.enterpriselibrary.logging.configuration.loggingsettings, microsoft.practices.enterpriselibrary.logging, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35" requirepermission="true" />   </configsections>   <instrumentationconfiguration performancecountersenabled="false"       eventloggingenabled="false" applicationinstancename="testapp" />   <loggingconfiguration name="" tracingenabled="true" defaultcategory="general">     <listeners>       <!-- using db trace listener because it's easy make fail -->       <add name="database trace listener" type="microsoft.practices.enterpriselibrary.logging.database.formatteddatabasetracelistener, microsoft.practices.enterpriselibrary.logging.database, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35"           listenerdatatype="microsoft.practices.enterpriselibrary.logging.database.configuration.formatteddatabasetracelistenerdata, microsoft.practices.enterpriselibrary.logging.database, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35"           databaseinstancename="database connection string" writelogstoredprocname="writelog"           addcategorystoredprocname="addcategory" />       <add name="flat file trace listener" type="microsoft.practices.enterpriselibrary.logging.tracelisteners.flatfiletracelistener, microsoft.practices.enterpriselibrary.logging, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35"           listenerdatatype="microsoft.practices.enterpriselibrary.logging.configuration.flatfiletracelistenerdata, microsoft.practices.enterpriselibrary.logging, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35"           filename="error.log" />       <add listenerdatatype="microsoft.practices.enterpriselibrary.logging.configuration.customtracelistenerdata, microsoft.practices.enterpriselibrary.logging, version=5.0.505.0, culture=neutral, publickeytoken=31bf3856ad364e35"           type="consoleapplication28.exceptionthrowingtracelistener, consoleapplication28, version=1.0.0.0, culture=neutral, publickeytoken=null"           name="exceptionthrowingtracelistener" />     </listeners>     <formatters />     <categorysources>       <add switchvalue="all" name="general">         <listeners>           <add name="database trace listener" />         </listeners>       </add>     </categorysources>     <specialsources>       <allevents switchvalue="all" name="all events" />       <notprocessed switchvalue="all" name="unprocessed category" />       <errors switchvalue="all" name="logging errors &amp; warnings">         <listeners>           <add name="flat file trace listener" />           <add name="exceptionthrowingtracelistener" />         </listeners>       </errors>     </specialsources>   </loggingconfiguration>   <connectionstrings>     <add name="database connection string" connectionstring="database=mydb"         providername="system.data.sqlclient" />   </connectionstrings>   <startup>     <supportedruntime version="v4.0" sku=".netframework,version=v4.0"/>   </startup> </configuration> 

now when run our test code exception (assuming database trace listener in config fails):

logwriter logwriter = enterpriselibrarycontainer.current.getinstance<logwriter>();  try {     logwriter.write("test", "general"); } catch (exception e) {     console.writeline("logwriter.write() threw exception: " + e); } 

now, that's long way go exceptions throwing. might better bite bullet , modify enterprise library source code directly adding throw statement.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -