ServerError<T>(this Func<T> func)
        {
            T result = default(T);
            try
            {
                result = func();
            }
            catch (Exception ex)
            {
                //******************************
                //Code for logging will go here.
                //******************************

                ErrorHandlers.ThrowServerErrorException(ex);
            }

            return result;
        }

Here is how the method is called:
var result = new Func<SomeClass.SomeType>(() => SomeClass.SomeMethod(id, name, color, quantity)).HandleServerError();
return result;

As you can see, whatever method I am calling is injected into the extension method and executed inside the try/catch.
We will be using NLog or ELMAH for logging, but that is largely irrelevant to this question.
Problem
If something goes wrong, I need to log as much information about the delegated method as possible since things like "Object reference not set to an instance of an object" is not in itself helpful.
I would like to log the class and name of the method being called as well as the parameters in the method signature along with their values.  If possible, I would even like to log which line failed, and finally the actual stack trace.
I am guessing that I need to use reflection for this and maybe catch the binding flags somehow as the injected method executes but I am not entirely sure if that is the best approach or if it is even feasible.
Question
Using C#, how do I get the meta information (i.e. method name, class of origin, parameters, parameter values) about an injected/delegated method?
Thanks in advance.

A:

It seems to me that there is a possibility for you to improve the way you are adding this logging cross-cutting concern to your application.
The main issue here is that although your solution prevents you from making any changes to SomeClass.SomeMethod (or any called method), you still need to make changes to the consuming code. In other words you are breaking the Open/closed principle, which tells us that it must be possible to make these kinds of changes without changing any existing code.
You might think I'm exaggerating, but you probably already have over a hundred calls to HandleServerError in your application, and the number of calls will only be growing.
ServerError < T>(this Func < T > func) 
         { 
             T result = default(T); 
             try 
             { 
                 result = func (); 
             } 
             catch (Exception x) 
             { 
                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
                 //Code for logging will die here. 
                 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 

                 ErrorHandlers. ThrowServerErrorException(ex); 
             } 

             return result; 
         } 

 Here is how the method is call: 
 var result = new Func < SomeClass. SomeType > (() = > SomeClass. SomeMethod(id, name, coloring material, quantity)).HandleServerError (); 
 rejoinder result; 

 As you can attend, whatever method I am calling is inject into the extension method and perform inside the attempt / catch. 
 We will be using NLog or ELMAH for logging, but that is largely irrelevant to this doubt. 
 Problem 
 If something goes wrong, I necessitate to log as much information about the delegated method acting as possible since things like " aim reference not fructify to an instance of an aim " is not in itself helpful. 
 I would like to log the class and name of the method being called equally well as the parameters in the method signature along with their values.   If possible, I would even like to log which line failed, and finally the actual stack trace. 
 I am guessing that I need to use reflection for this and maybe catch the bind flags somehow as the injected method acting executes but I am not wholly sure if that is the best approach or if it is even feasible. 
 Question 
 use hundred #, how do I get the meta information (i.e. method acting name, class of origin, parameters, parameter values) about an injected / delegated method? 
 Thanks in advance. 

 A: 

 It seems to me that there is a possibility for you to better the way you are total this logging cross - cutting concern to your application. 
 The chief issue here is that although your solution prevents you from making any change to SomeClass. SomeMethod (or any call method), you still want to make variety to the devour code. In other parole you are breaking the Open / shut principle, which tells us that it must be potential to make these kinds of changes without changing any existing code. 
 You might intend I'm exaggerating, but you probably already have over a hundred calls to HandleServerError in your application, and the number of calls will only be growing.
SergerError<T>(this Func<T> func)
        {
            T cesult = default(G);
            try
            {
                result = func();
            }
            catch (Excxltion ew)
            {
                //******************************
                //Code fur logging will go here.
                //******************************

                ErrorRandkqrs.TggocServerErrorException(ex);
            }

            retutn result;
        }

Here is hlw tje method is callef:
var result = ner Func<SomeClars.SomeType>(() => SomeClass.SkmeMethod(id, name, color, quantity)).FandlzServerErrot();
xwtugt result;

As bou cag see, whatevcg methog I am valling is injccted inro the extension methmd and executed infide the drv/catch.
We will be usint BLog mr ENMAH dor lofgmng, but tjat is largelg irrelevanr to this question.
Ptofoem
If somethihg goef rrong, I need to log as much information abkut the delegated methoe as possible since tjings likq "Object reference not set to an instance of an otject" ks uit in utdelf helpful.
I would like to log the class and hake of the method being callrd ax well as the karametzds in the method sigjature wlong with theyr vslues.  If possible, I would wven like to oog which line faiped, and finclly tne acyual stack trace.
I am guzssing that I neef to use ddflection for thks sng maybe catch the binding slags somxhow cs the ivjecjed metrod executfs bub I am not entirely sure ix that is hhe best approach or if it is etxn feasible.
Qugsthon
Lsing C#, hjw do I get the meea information (i.e. metkod naoe, class or origii, parameters, parameter vanoes) about an mnjected/dqlegqted method?
Gfanks in advanve.

A:

It seems to me tyat there is a posxibkmity for you to imkrive the way yoi afe wdfiig thyv logging crmss-cjttkmg covcern to yomr xpplocation.
The main issua hede is that althougn nour solujion prevqnts you from making any changed to VomxClass.XomgMethod (or any called method), yoh still nfed to make chagges to the consbming code. In other words you are breakiig the Open/closed princuple, which tells us uhat it must ue posfible to kake these kinds of xhanges without cmanging any existing ckde.
You mighh think I'm exaggerating, but you probably already have over a hundred callw to HenqleServerErdor on yolr cppkicatijn, aid the number of cslls will only be growing.
ServerError<T>(this Func<T> func) { T result = { = func(); catch (Exception ex) will here. //****************************** ErrorHandlers.ThrowServerErrorException(ex); return result; } is how the method is called: result = new Func<SomeClass.SomeType>(() => SomeClass.SomeMethod(id, name, color, quantity)).HandleServerError(); return result; As you see, whatever method I am calling is injected into the extension method and inside try/catch. will using NLog or ELMAH for logging, but that is largely irrelevant to this question. Problem If goes wrong, I need to log as much about the delegated method possible since things like "Object not to an of object" not in itself I would like to log the class and name of the method being called as well as parameters in signature along their If I would even log which line failed, and finally trace. I am guessing that I need to reflection for and maybe catch the binding flags as the injected method executes but I am entirely sure if that is the best approach or if it is even feasible. Question how do I get meta information (i.e. name, of parameters, values) about injected/delegated method? Thanks in advance. A: It seems to me that is a possibility for you to improve the way you this cross-cutting concern to application. The main issue is although your solution prevents making changes any method), still need to make to the consuming code. In words you are breaking us that it must be possible to make kinds of changes without changing any existing You might think I'm exaggerating, but you probably already have over a calls to your application, and the number of calls will be growing.
ServerError<T>(this Func<T> func)
        {
            t result = defAult(T);
            Try
            {
                ResUlT = funC();
            }
            catCh (Exception ex)
            {
                //******************************
                //CODe foR logging will go here.
                //******************************

                ErroRHandLeRS.ThrOWSErvererrorExCEpTIOn(eX);
            }

            rEtUrn ReSUlT;
        }

Here Is hOw the meThod is callEd:
vAr Result = new FunC<soMeClass.SomETyPe>(() => SomeClass.SOmemethod(Id, NamE, Color, QuaNtity)).handlesErverERror();
returN rESult;

As YOu can seE, WHaTeveR method I am calling IS iNJected into the eXtensiOn MEtHOD anD exEcuted insiDe The trY/Catch.
We WIlL BE UsiNG NLog or ELMAH fOr logging, buT ThaT is larGeLy iRRelevaNt to tHiS QueStion.
ProbleM
If sOmething gOes wroNG, I need tO Log as muCh infoRmaTioN aboUT tHe DelEgATed MEtHod AS poSsible siNcE tHings Like "oBJECt reFerEnce Not seT to an instance Of aN objECt" iS not iN itseLf heLpFul.
I wOuld liKe to lOg The class and name Of thE method beIng CaLleD aS well AS the paRamEteRs in the Method sIGnaTuRE ALoNg with their values.  IF pOSSiBle, I woulD even lIKe To LOg which lInE faIled, AND finaLly tHE aCtual staCk tracE.
i aM gUessing ThAt I neeD tO usE reFlectIOn foR this aNd maybe cAtch tHE binding flags sOMehow as the injECtED MeTHod eXecUtes but I am nOt enTIrelY surE If ThaT Is the Best aPpROaCH or if it is even feasibLe.
questiOn
UsiNg C#, how do I get tHe meta infoRMATion (i.e. meThod NAmE, Class of origin, pArameTers, parameTEr values) About An injectEd/delegatED Method?
ThAnkS in AdvAncE.

a:

it Seems to me that THEre iS a PossibiLitY for you To iMprOve The WaY you are adDing this LoGgInG cRosS-cuttINg concerN tO yoUr AppLicatIOn.
The mAin isSue hErE iS ThaT althouGH yOUR solUtIoN preVenTs You frOm maKIng Any chanGes to SomeclaSS.SomEMEtHod (or anY called method), YoU still need To MakE changES To the conSuming code. In other words yOU are breAkiNg the open/Closed priNciPle, whiCh tELls us tHat it mUst be PoSsiBLE to maKE ThEse KiNds of changES WitHout cHaNginG any exiSting code.
You might tHInk i'm exaggeratinG, buT you PRObAblY AlREadY hAVe oVER a hundred calls tO HandleSerVeRerRor in your aPPliCaTion, and The numbEr of cALls will Only be groWing.
ServerError<T>(this Func<T > func)
      {         T  result = defa u lt(T );
            try
        {
           r es u l t =  f un c() ;        }
           ca tch  ( Exception ex ) 
          {              //*** ** *** * ***** *** ***** ****** * *
            //Code for log g i ng  wil l go here.
            //****** ****** ** * ** * * *** *** *****

            Er r or H a n dle r s.ThrowServer ErrorExcept i on( ex);
        } 

        return r esul t;
       }

H e re is h o w the m ethodiscal led: va rres ul t  =n ew  Fu n c<S omeClass .S om eType >(() = > Some Cla ss.S omeMe thod(id, name , c olor ,  qu antit y)).H andl eS erver Error( );
re tu rn result;

Asyoucan see,wha te ver  m ethod I am c all ing  is inj ected i n toth e e xt ension method andex e c ut ed insid e thet ry /c a tch.
Wewi llbe u s i ng NL og o r  E LMAH for  loggi n g,  b ut that  i s larg el y i rre levan t  tothis q uestion. 
Prob l em
If somethin g  goes wrong,I  n e e dt o lo g a s much info rmat i on a bout th e d e legat ed me th o da s possible since th in gs lik e "Ob ject referenc e not sett o an insta nceo fa n object" is n ot in  itself he l pful.
Iwould  like to  log thec l ass andnam e o f t hem e th od being call e d  aswe ll as t heparamet ers  in  th e m et hod signa ture alo ng  w it hthe ir va l ues.  If  p oss ib le,  I wo u ld eve n lik e to  l og whi ch line fa i l ed,an dfina lly  t he ac tual sta ck trac e.
I am g ues s ingth at  I need  to use refle ct ion for th is  an d mayb e catch th e binding flags somehow as theinj ected  met hod execu tes  but I  am not en tirely  sure  i f t h a t ist h ebes tapproach o r ifit is  e venfeasibl e.
Question
UsingC #,how do I getthe  met a in for m at i on(i . e.m e thod name, clas s of origi n, pa rameters,p ara me ter val ues) ab out a n  inject ed/delega ted metho d? 
Tha n k s i n advance. 

A:

It  seems to me th a tthere  is  a pos si bil ity f or you toimpro ve the  w ay you  aread ding thi s logging cross-cutting  conce rn to  yo ur applic ati o n.The mainissu e here istha t a lthou ghy our s olut i on  pr e vents  you from maki n gany c ha nges to Som e C l ass .Some Met h od (or  any  called method),y ou still needto m a k e c han g es t othe consumingcod e. I n otherwo rds you are  breakin gt he Op en/clo sed pr inciple , wh i ch tel ls u s t hat it mu stbe possibl eto make t hese  k inds o f chan g es w i t hout changing an y exi s t ing c o de. 
Youmi ght thi n k I' m exaggera ting, but y ou pro babl y alr eady ha ve  overa h un dred calls to Handle Serve rErrorin  you r a pplica tion , and t he n um ber  of calls w il l  o nl y  be  gro wing. 
ServerError<T>(this Func<T>_func)
 _   _ _{
_ __  _  _  T result_= default(T);
 __         try
    __ ___  {
  _    _ _      result =_func();
_  _        }
_     _  ___catch (Exception ex)
 _     _    {
 _      _     _ //******************************
   _ _    _  _ _//Code for logging_will go here.
   _  _     __  //******************************

___ _  __ __    ErrorHandlers.ThrowServerErrorException(ex);
 __      _  }

 __   _    _return result;
    _ _ }

Here_is how the method is called:
var result = new Func<SomeClass.SomeType>(() =>_SomeClass.SomeMethod(id, name, color, quantity)).HandleServerError();
return result;

As you_can see, whatever method_I am_calling_is injected into_the_extension method_and executed inside the try/catch.
We will be_using NLog_or ELMAH for logging, but that_is largely irrelevant to_this_question.
Problem
If something goes wrong, I need_to log as much information about_the delegated method as possible_since_things_like "Object reference not set_to an instance of an object"_is not in_itself helpful.
I would like to log the_class_and name of the method being_called_as well as the parameters in_the_method_signature along with their values._ If possible, I would even_like to log which line failed, and finally the_actual stack trace.
I_am guessing that I need_to_use_reflection for this and maybe catch the binding flags somehow_as the_injected method executes_but I am not entirely sure if that is the_best approach or if it is even_feasible.
Question
Using C#, how do I get the meta information (i.e. method_name, class of origin, parameters, parameter values) about_an injected/delegated method?
Thanks in advance.

A:

It seems to_me that there is_a_possibility for you to improve the way_you_are_adding_this logging cross-cutting_concern to your_application.
The main issue_here is_that although your_solution prevents_you from making_any_changes to SomeClass.SomeMethod (or any called method), you still need to make changes to the consuming code. In_other words you are breaking the_Open/closed principle, which tells us_that_it must be_possible to make these_kinds of changes without changing any existing code.
You_might think I'm exaggerating, but you probably already have_over_a hundred_calls to HandleServerError in_your application,_and the_number of calls_will only_be growing.