以Xml Formate存储错误日志 [英] Storing the Error Logs in Xml Formate

查看:89
本文介绍了以Xml Formate存储错误日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我做了存储错误日志的

使用Path,ErrorMessage,MethodName,LineNumber.

但是我也想存储variableName,我该怎么办呢?

例如:
我有方法
方法中有3个参数.如果错误来自3参数,我想存储3 parameterName(variableName).


我该怎么办?

这是textformate


我的代码

//class

  class 程序
  {
      静态 字符串 i = "  sdf";

      静态 无效 Main(字符串 []参数)
      {
          尝试
          {
              // 但是我要在这行中存储41行,我该怎么办
              测试(" "  TCS"捕获(例外)
          {
              CreateLogFiles Err =  CreateLogFiles();
              Err.ErrorLog("   MainMethod",例如消息,例如StackTrace.Substring(ex.StackTrace.LastIndexOf( ))));
          }
          最终
          {
              Console.ReadLine();
          }

      }

      静态 无效 Testing(字符串 str,字符串 str1, int  integerValue)
      {
          字符串 s = str;
          字符串 ss = str1;
          // 实际上这是错误的,所以我想存储第三个参数integerValue 
          integerValue = Convert.ToInt32(" );
           int  IntValue = integerValue;
      }

       CreateLogFiles
      {
          私有 字符串 sLogFormat;
          私有 字符串 sErrorTime;

          公共 CreateLogFiles()
          {
              // 用于创建日志文件格式的sLogFormat:
              //  dd/mm/yyyy hh:mm:ss AM/PM ==>日志消息
              sLogFormat = DateTime.Now.ToShortDateString().ToString()+ "  + DateTime.Now.ToLongTimeString ().ToString()+ " ;

              // 此变量用于创建日志文件名格式"
              // 例如文件名:ErrorLogYYYYMMDD 
              字符串 sYear = DateTime.Now.Year.ToString();
              字符串 sMonth = DateTime.Now.Month.ToString();
              字符串 sDay = DateTime.Now.Day.ToString();
              sErrorTime = sYear + sMonth + sDay;
          }

          公共 无效 ErrorLog(字符串 sPathName, string 方法名称, string  sErrMsg, string  LineNo)
          {
              StreamWriter sw =  StreamWriter(sPathName, true );
              sw.WriteLine("  + sLogFormat +  "  + "  + MethodName +   \ n" +  错误消息:" + sErrMsg +   + "  + LineNo);
              sw.Flush();
              sw.Close();
          }
      }

  }

解决方案

有关特定方法,请参见以下代码:

 使用系统;
使用 System.Text;
使用 System.Reflection;
使用 System.Collections;
使用 System.Diagnostics;

命名空间异常调试
{
     class 程序
    {
        公共 静态  void  EronousMethod(字符串 str,字符串 str1, int  integerValue)
        {
            尝试
            {
                抛出 异常(" 某些错误");
            }
            捕获(例外)
            {
                ex.Data [" ] = str;
                ex.Data [" ] = str1;
                ex.Data [" ] = integerValue;

                投掷;
            }
        }

        公共 静态  void  Main(字符串 []参数)
        {
            尝试
            {
                EronousMethod(" "  bar"捕获(例外)
            {
                StringBuilder sb =  StringBuilder();
                 foreach (DictionaryEntry对 in 中的ex.Data)
                {
                    sb.Append(字符串 .Format(" ,pair.Key,pair.Value.ToString()));
                }
                Console.WriteLine(sb.ToString());
            }
        }
    }
} 



您可以根据需要进行调整.据我所知,没有直接的方法来检索(枚举)运行时参数值,因此您需要为每个受监视的函数填充 Data 属性.

此处介绍了一种更通用的方法: http://stackoverflow.com/questions/2405230/can-i-get-parameter-names-values-procedurally-from-the-currently-executing-funct [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(); } } }

See following code for a specific approach:

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.


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.


这篇关于以Xml Formate存储错误日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆