Generic logging of function parameters in exception handling

后端 未结 7 1072
渐次进展
渐次进展 2020-12-14 01:15

A lot of my C# code follows this pattern:

void foo(string param1, string param2, string param3)
{
    try
    {
         // do something...
    }
    catch(E         


        
7条回答
  •  [愿得一人]
    2020-12-14 01:56

    When I have done this I just created a generic dictionary for the logging.

    I have this LogArgs class. And logging in a base class that I call when I have an exception.

    public class LogArgs
    {
    
        public string MethodName { get; set; }
        public string ClassName { get; set; }
        public Dictionary Paramters { get; set; }
    
    
        public LogArgs()
        {
            this.Paramters = new Dictionary();
        }
    
    }
    

    Then at the start of every method I do

    LogArgs args = new LogArgs { ClassName = "ClassName", MethodName = "MethodName" };
    args.Paramters.Add("Param1", param1);
    args.Paramters.Add("Param2", param2);
    args.Paramters.Add("Param3", param3);
    
    base.Logger.MethodStartLog(args);
    

    When I have an error I log it this way.

    base.Logger.LogError(args, ex);
    

提交回复
热议问题