一、工程错误日志管理方案设计
主要采用SystemInfo.ini设备文件存储标识:
DEBUG状态弹出异常;
RELEASE状态直接将异常信息写入日志文件而不弹出窗口。
调试状态下,
[Setting]
IsShowException=True
运行效果:
发布状态下,
[Setting]
IsShowException=False
运行效果:
二、关键类设计
CIniFileIO.cs类用于读INI文件的IsShowException关键字
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO;
-
using System.Runtime.InteropServices;
-
-
namespace TestGetSelfNameAPI
-
{
-
public class CIniFileIO
-
{
-
[DllImport("kernel32")]
-
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
-
[DllImport("kernel32")]
-
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
-
[DllImport("kernel32")]
-
private static extern int GetPrivateProfileString(string section, string key, string def, byte[] BufferRe, uint size, string FilePath);
-
-
-
#region =============-------Write and Read IsShowException ---------================
-
public static bool GetIsShowException()
-
{
-
bool isShow = true;
-
FileInfo sysInfo = new FileInfo(System.Windows.Forms.Application.StartupPath + "\\SystemInfo.ini");
-
string sectionName = "Setting";
-
string keyName = "IsShowException";
-
string truthValue = "True";
-
-
if (sysInfo.Exists)
-
{
-
StringBuilder strBuilder = new StringBuilder(255);
-
GetPrivateProfileString(sectionName, keyName, "", strBuilder, 255, sysInfo.ToString().Trim());
-
truthValue = strBuilder.ToString();
-
if (truthValue.Equals("True"))
-
isShow = true;
-
else
-
isShow = false;
-
}
-
-
return isShow;
-
}
-
-
public static void WriteIsShowException(bool isShowException)
-
{
-
/* 为DayaSystemInof.ini文件写进信息,来进行登记 */
-
string iniFilePath = System.Windows.Forms.Application.StartupPath + "\\SystemInfo.ini";
-
string sectionName = "Setting";
-
string keyName = "IsShowException";
-
string truthValue = "True";
-
-
if (isShowException)
-
truthValue = "True";
-
else
-
truthValue = "False";
-
string keyValue = truthValue;
-
WritePrivateProfileString(sectionName, keyName, keyValue, iniFilePath);
-
}
-
#endregion
-
}
-
}
CLogManager.cs用于写错误日志文件
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.IO;
-
using System.Threading;
-
using System.Net;
-
using System.Windows.Forms;
-
using System.Web;
-
using System.Diagnostics;
-
-
namespace TestGetSelfNameAPI
-
{
-
///
-
/// 通过检测DayaSystemInfo.ini目录下的IsShowException关键字
-
/// 以决定是否弹出异常
-
///
-
public class CLogManager
-
{
-
private static string mLogFilePath = null;
-
private static object mConcreteObject = new object();
-
private static string mStrExecutePath = null;
-
private static StreamWriter mLogStringWriter = null;
-
private static short mLoggerLevel = 0;
-
-
static CLogManager()
-
{
-
mStrExecutePath = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf(@"\") + 1);
-
if (!Directory.Exists(mStrExecutePath + "LogMessage"))
-
{
-
Directory.CreateDirectory(mStrExecutePath + "LogMessage");
-
}
-
mLogFilePath = (mStrExecutePath + @"LogMessage\ExceptionRecordLog.txt");
-
-
if (!File.Exists(mLogFilePath))
-
{
-
FileStream fs = File.Create(mLogFilePath);
-
fs.Dispose();
-
fs.Close();
-
}
-
try
-
{
-
string level = System.Configuration.ConfigurationSettings.AppSettings["mLoggerLevel"];
-
mLoggerLevel = short.Parse(level);
-
}
-
catch
-
{
-
mLoggerLevel = 0;
-
}
-
}
-
-
///
-
/// delete logs which have not been updated for more than 15 days
-
///
-
public static void LogClean()
-
{
-
/* 获取十五天前的日期 */
-
string fifteenDaysbefore = DateTime.Now.AddDays(-15.0).ToString("yyyyMMdd");
-
string fiveminutebefore = DateTime.Now.AddMinutes(-5.0).ToString("yyyyMMddHHmmSS");
-
/* 应用程序目录+LogMessage文件夹 */
-
string[] logs = Directory.GetFiles(mStrExecutePath + "LogMessage");
-
-
foreach (string log in logs)
-
{
-
System.IO.FileInfo info = new FileInfo(log);
-
-
if (info.LastWriteTime.ToString("yyyyMMdd").CompareTo(fifteenDaysbefore) < 0)
-
{
-
try
-
{
-
File.Delete(log);
-
}
-
catch { }
-
}
-
}
-
}
-
-
-
///
-
/// ExceptionHandle 重载函数一
-
///
-
public static void ExceptionHandle(string csFileName, string funcName, Exception exceptionName)
-
{
-
/* 同步访问对象方式 */
-
Monitor.Enter(mConcreteObject);
-
/* 创建前一天的日期文件(名) */
-
string strLogBakPath = mStrExecutePath + @"LogMessage\" + DateTime.Now.AddDays(-1.0).ToString("yyyyMMdd") + ".txt";
-
-
if (!File.Exists(strLogBakPath))
-
{
-
/* Copy是非覆盖拷贝,所以要做文件存在性的判断
-
取mLogFilePath,代表全局静态的NetConnectorLog.txt文件的值给前天的日期文件!*/
-
File.Copy(mLogFilePath, strLogBakPath);
-
}
-
-
/* mLogStringWriter是一个IO流对象,没有就创建一个StreamWriter;*/
-
if (mLogStringWriter == null)
-
{
-
/* 取mLogFilePath上的文件,生成一个bak备份文件 */
-
strLogBakPath = mStrExecutePath + @"LogMessage\" + DateTime.Now.ToString("yyyyMMddHHmmsss") + ".txt.bak";
-
File.Copy(mLogFilePath, strLogBakPath);
-
try
-
{
-
mLogStringWriter = new StreamWriter(mLogFilePath);
-
}
-
catch { }
-
}
-
try
-
{
-
mLogStringWriter.WriteLine("<Exceptoin occurred in " + csFileName + "'s " + funcName + "() At " + string.Format("{0}", DateTime.Now.ToString()) + ">");
-
mLogStringWriter.WriteLine("**********************************************************************************************************************");
-
mLogStringWriter.WriteLine(string.Format("{0}", exceptionName.ToString()));
-
mLogStringWriter.WriteLine("----------------------------------------------------------------------------------------------------------------------");
-
mLogStringWriter.WriteLine();
-
mLogStringWriter.WriteLine();
-
mLogStringWriter.Flush();
-
}
-
finally
-
{
-
/* 释放排他锁 */
-
Monitor.Exit(mConcreteObject);
-
}
-
}
-
-
///
-
/// 接收上层传进来的异常信息
-
///
-
/// 包含发生异常的函数名及所有的文件的名称
-
/// 整个异常的信息
-
public static void SendException(StackTrace st, Exception exceptionName)
-
{
-
string csFileName = string.Empty;
-
string fileFullName = string.Empty;
-
string methodName = string.Empty;
-
-
StackFrame sf = st.GetFrame(0);
-
fileFullName = sf.GetFileName();
-
csFileName = fileFullName.Substring(fileFullName.LastIndexOf('\\') + 1);
methodName = sf.GetMethod().Name;
if (CIniFileIO.GetIsShowException())
MessageBox.Show(exceptionName.ToString());
else
{
ExceptionHandle(csFileName, methodName, exceptionName);
LogClean();
}
}
public static void DirectWirteErrorToLog(string csFileName, string methodName, Exception exceptionName)
{
ExceptionHandle(csFileName, methodName, exceptionName);
LogClean();
}
}
}
源工程代码:
TestGetSelfNameAPI.rar
三、参考文献
获取当前函数名及函数所在的文件
http://www.cnblogs.com/hyddd/articles/1500808.html
阅读(2832) | 评论(0) | 转发(0) |