EDN Admin
Well-known member
<p align=left><font face=Arial size=2>Hello,</font>
<p align=left>I need to create a custom attribute class that will help me log the beginning and completion of a method, as well as any error of type Exception.
<p align=left>
<p align=left>Ideally, it will look something like this
<p align=left>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Block
<p align=left>[Loggable(OnStart=true, OnEnd=true, OnError=false)]
<p align=left>pubilc class A
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>public void DoSomething() // Inherits logging attribute from class
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>
<p dir=ltr align=left>[NotLoggable]
<p dir=ltr align=left>public void DoSomethingElse() // Doesnt log, or equivalent to [Loggable(OnStart=false, OnEnd=false, OnError=false)]
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>
<p dir=ltr align=left>[Loggable(OnError=true)] // Inherits from class, except OnError=true.
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>} </blockquote>
<p dir=ltr align=left>}
<p align=left>
<p align=left>
<p align=left>I know how to define a custom attribute (have a book). What Im not sure is about methods/properties inheritting an attribute from their classes.
<p align=left>
<p align=left>Also, I need to actually do something with the attributes (log), so what I basically need is to provide for an action to occur before running a method (i.e. log the method name, parameters and start time), after the method returns (log the method name, return value if any, and end time), and if the method throws an exception, log it, the errorMessage and stack-trace.
<p align=left>It should be equivalent to this:
<p align=left>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Block
<p align=left>try
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>LogBegin(method name, parameters[], DateTime.Now);
<p align=left>retVal = DoSomething(paramaters[]);
<p align=left>LogEnd(method name, retval, DateTime.Now); </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>catch (ApplicationException ex)
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// Do not log this - it is less important
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>catch (Exception ex)
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// Log this
<p dir=ltr align=left>LogError(method name, ex.Message, ex.type, ex.stackTrace); </blockquote>
<p dir=ltr align=left>}
<p align=left>
<p align=left>
<p dir=ltr align=left>Is this possible? I assume this isnt much different from a Transaction attribute.
<p dir=ltr align=left>I dont want some one to write my code for me, of course, but a tip on how to do this, or an example I can use to extrapolate my code would be greatly appreciated.
<p dir=ltr align=left>
<p dir=ltr align=left>Thanks,
<p dir=ltr align=left>Assaf.
<p dir=ltr align=left>
<p dir=ltr align=left>
View the full article
<p align=left>I need to create a custom attribute class that will help me log the beginning and completion of a method, as well as any error of type Exception.
<p align=left>
<p align=left>Ideally, it will look something like this
<p align=left>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Block
<p align=left>[Loggable(OnStart=true, OnEnd=true, OnError=false)]
<p align=left>pubilc class A
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>public void DoSomething() // Inherits logging attribute from class
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>
<p dir=ltr align=left>[NotLoggable]
<p dir=ltr align=left>public void DoSomethingElse() // Doesnt log, or equivalent to [Loggable(OnStart=false, OnEnd=false, OnError=false)]
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>
<p dir=ltr align=left>[Loggable(OnError=true)] // Inherits from class, except OnError=true.
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>} </blockquote>
<p dir=ltr align=left>}
<p align=left>
<p align=left>
<p align=left>I know how to define a custom attribute (have a book). What Im not sure is about methods/properties inheritting an attribute from their classes.
<p align=left>
<p align=left>Also, I need to actually do something with the attributes (log), so what I basically need is to provide for an action to occur before running a method (i.e. log the method name, parameters and start time), after the method returns (log the method name, return value if any, and end time), and if the method throws an exception, log it, the errorMessage and stack-trace.
<p align=left>It should be equivalent to this:
<p align=left>
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Block
<p align=left>try
<p align=left>{
<blockquote dir=ltr style="margin-right:0px
<p align=left>LogBegin(method name, parameters[], DateTime.Now);
<p align=left>retVal = DoSomething(paramaters[]);
<p align=left>LogEnd(method name, retval, DateTime.Now); </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>catch (ApplicationException ex)
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// Do not log this - it is less important
<p dir=ltr align=left>// ... </blockquote>
<p dir=ltr align=left>}
<p dir=ltr align=left>catch (Exception ex)
<p dir=ltr align=left>{
<blockquote dir=ltr style="margin-right:0px
<p dir=ltr align=left>// Log this
<p dir=ltr align=left>LogError(method name, ex.Message, ex.type, ex.stackTrace); </blockquote>
<p dir=ltr align=left>}
<p align=left>
<p align=left>
<p dir=ltr align=left>Is this possible? I assume this isnt much different from a Transaction attribute.
<p dir=ltr align=left>I dont want some one to write my code for me, of course, but a tip on how to do this, or an example I can use to extrapolate my code would be greatly appreciated.
<p dir=ltr align=left>
<p dir=ltr align=left>Thanks,
<p dir=ltr align=left>Assaf.
<p dir=ltr align=left>
<p dir=ltr align=left>
View the full article