2

Actually, I want to log the data in such a way that it should have the methods that the application goes through in c#, and if there is an error then the error content also should be logged. the problem is where to call the log methods inside of catch or inside of every method? as I have nearly 200 methods.

I wrote the code like this:

    public static bool Logging(System.Reflection.MethodBase methodInfo)
     {
        var fullMethodName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;
        if (error_flag == false)
        {
            using (StreamWriter outputFile = new StreamWriter(path + @"\log.txt", true))
            {
                outputFile.WriteLine(fullMethodName + ": OK");
            }
        }
        else
        {
            using (StreamWriter outputFile = new StreamWriter(path + @"\log.txt", true))
            {
                outputFile.WriteLine("\n\n --> Error in : " + fullMethodName);
            }
        }
        return true;
    }

    //Logging Method 2
    public static bool WriteErrorLog(Exception ErrorMessage)
    {
        using (StreamWriter outputFile = new StreamWriter(path + @"\log.txt", true))
        {
            outputFile.WriteLine("{0} Exception caught.", ErrorMessage);
        }
        return true;
    }

and I have to call those methods from where??

  • Possible duplicate of [log4net log all unhandled application errors](http://stackoverflow.com/questions/1367967/log4net-log-all-unhandled-application-errors) – Martheen Aug 25 '16 at 08:43

1 Answers1

1

I would suggest the following approach:

static void Main()
{
    try
    {
        Log.Info("Start of process");

        string input = StepOne();
        StepTwo(input);

        Log.Info("End of process");
    }
    catch(Exception e)
    {
        Log.Error(e);
    }
}

public static string StepOne()
{
    Log.Info("StepOne Completed");

    return "Step One";
}

public static void StepTwo(string input)
{
    Log.Info("StepTwo, input: " + input);

    throw new ArgumentNullException("input");
}       
  1. Rather than rolling your own, use an existing logging framework, for example Log4Net.
  2. Add a try catch at the highest possible layer, let errors bubble up until you can actually do something sensible with them.
  3. Add logging messages to functions where it is sensible to do so, this will be the most useful information, for example you can log method inputs.
  4. Avoid using reflection to get the method name, you probably don't need to log individual method names. All of that information will be in the stack trace anyway.
James Wood
  • 17,286
  • 4
  • 46
  • 89
  • where can I get Stack trace? in visual studio or somewhere? – Bhoopathi Reddy Sep 06 '16 at 13:29
  • @BhoopathiReddy, its a property of the `Exception` object, `ex.StackTrace` - https://msdn.microsoft.com/en-us/library/system.exception.stacktrace(v=vs.110).aspx. – James Wood Sep 06 '16 at 16:03
  • Please answer to this also, Thanks in advance: http://stackoverflow.com/questions/39526161/how-to-create-a-new-log-file-every-time-when-the-application-starts-using-lo4net – Bhoopathi Reddy Sep 16 '16 at 07:45