Click here to Skip to main content
15,895,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
I did the storing the error logs

with Path,ErrorMessage,MethodName,LineNumber.

but i want to store the variableName also how can i do this?

ex:
i have method
in method have 3parameters. if error came from 3 parameter i want to store the 3 parameterName(variableName).


how can i did this?

this is textformate


my code

//class
C#
class Program
  {
      static string i = "sdf";

      static void Main(string[] args)
      {
          try
          {
              //But stacktrace this line i want to store 41 line alos how can i do
              Testing("Hi", "TCS", 2);
              Console.ReadLine();
          }
          catch (Exception ex)
          {
              CreateLogFiles Err = new CreateLogFiles();
              Err.ErrorLog("F:\\Test.txt", "MainMethod ", ex.Message, ex.StackTrace.Substring(ex.StackTrace.LastIndexOf("line")));
          }
          finally
          {
              Console.ReadLine();
          }

      }

      static void Testing(string str, string str1, int integerValue)
      {
          string s = str;
          string ss = str1;
          //Actually this is error so i want to store 3rd parameter integerValue
          integerValue = Convert.ToInt32("test");
          int IntValue = integerValue;
      }

      class CreateLogFiles
      {
          private string sLogFormat;
          private string sErrorTime;

          public CreateLogFiles()
          {
              //sLogFormat used to create log files format :
              // dd/mm/yyyy hh:mm:ss AM/PM ==> Log Message
              sLogFormat = DateTime.Now.ToShortDateString().ToString() + " " + DateTime.Now.ToLongTimeString().ToString() + " ==> ";

              //this variable used to create log filename format "
              //for example filename : ErrorLogYYYYMMDD
              string sYear = DateTime.Now.Year.ToString();
              string sMonth = DateTime.Now.Month.ToString();
              string sDay = DateTime.Now.Day.ToString();
              sErrorTime = sYear + sMonth + sDay;
          }

          public void ErrorLog(string sPathName, string MethodName, string sErrMsg, string LineNo)
          {
              StreamWriter sw = new StreamWriter(sPathName, true);
              sw.WriteLine("Date Time : " + sLogFormat + "\n" + "Method Name : " + MethodName + "\n" + "Error Message : " + sErrMsg + "\n" + "Line No : " + LineNo);
              sw.Flush();
              sw.Close();
          }
      }

  }
Posted
Updated 16-Aug-12 23:24pm
v3
Comments
Zoltán Zörgő 17-Aug-12 5:12am    
Where is "variablename" coming from?
Ps: what has this to do with XML?
Better use nlog: http://nlog-project.org/
U@007 17-Aug-12 5:24am    
see updated my codeing.
Zoltán Zörgő 17-Aug-12 5:33am    
So you want to log not only the method, that had thrown the exception, but also the actual parameter list? I think can be achieved, but will be not simple, and might be considerable performance overhead.
Zoltán Zörgő 17-Aug-12 5:39am    
Do you want a general approach, or only for a specific method?

See following code for a specific approach:
C#
using System;
using System.Text;
using System.Reflection;
using System.Collections;
using System.Diagnostics;

namespace exceptiondebug
{
    class Program
    {
        public static void EronousMethod(string str, string str1, int integerValue)
        {
            try
            {
                throw new Exception("Some error");
            }
            catch (Exception ex)
            {
                ex.Data["str"] = str;
                ex.Data["str1"] = str1;
                ex.Data["integerValue"] = integerValue;

                throw;
            }
        }

        public static void Main(string[] args)
        {
            try
            {
                EronousMethod("foo","bar",40);
            }
            catch (Exception ex)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DictionaryEntry pair in ex.Data)
                {
                    sb.Append(String.Format("{0}:{1}\n",pair.Key, pair.Value.ToString()));
                }
                Console.WriteLine(sb.ToString());
            }
        }
    }
}


You can adapt it to your needs. As I know, there is no direct way, to retrieve (enumerate) runtime parameter values, thus you need to fill Data property for every watched function.

A more general approach is described here: http://stackoverflow.com/questions/2405230/can-i-get-parameter-names-values-procedurally-from-the-currently-executing-funct[^] using PostSharp. You can use this approach, to store context of the watched method somewhere, and log it if needed. But as I said it is a considerable overhead.

It would be great if somebody would know a way, to enumerate or retrieve method parameters by name, at runtime even from inside the method itself.
 
Share this answer
 
You are not writing XML. Either way, you can store any values you like. Your question is not clear, but if ou want to store another value, write it to your stream. Add another parameter to the method, and pass it in and write it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900