📄 defaultloghandler.cs
字号:
using System;
using ExManagement.Interface;
namespace ExManagement.LogHandler
{
/// <summary>
/// 框架提供的默认日志记录器类, 范例
/// </summary>
public class DefaultLogHandler:IExLogHandler
{
/// <summary>
/// 进行截取过滤以获取错误代码行
/// </summary>
/// <param name="e">系统异常</param>
/// <returns>代码行位置信息</returns>
private string GetExceptionLocation(System.Exception e )
{
int iLen , iReturnPos;
string strErr , sLocateTemp , sLocate = "" ;
//获取源异常信息
strErr = e.StackTrace;
#if DEBUG
if( strErr != string.Empty && strErr != null)
{
iLen = strErr.Length ;
//查找第一行,其中行以回车结束
iReturnPos = strErr.IndexOf( "\r\n" , 0 , iLen);
while(iReturnPos > 0 && strErr.Length > 1)
{
//判断当前读取的行中有无 line 字样 , 表示出错的具体代码行
sLocateTemp = strErr.Substring(0,iReturnPos);
if(sLocateTemp.IndexOf("line",0,sLocateTemp.Length ) > 0)
sLocate += sLocateTemp + Environment.NewLine ;
//将已判断的行删除,取下一行数据,加4表示/r/n四位
strErr = strErr.Substring(iReturnPos + 4 , iLen - iReturnPos - 4);
iLen = strErr.Length ;
iReturnPos = strErr.IndexOf( "\r\n" , 0 , iLen);
}
if(strErr.IndexOf("line",0,strErr.Length ) > 0)
sLocate += strErr + Environment.NewLine ;
}
#else
if( strErr != string.Empty && strErr != null)
{
if(strErr.IndexOf( "\r\n") == -1 )
{
sLocate = strErr;
}
else
{
if(strErr.IndexOf( "\r\n") > 0)
{
sLocate = strErr.Substring(strErr.LastIndexOf("\r\n"));
}
}
}
#endif
return sLocate.Trim();
}
public int ExLog(Exception ex, string strErrorCode, string strLayerName, string strUserId, params string [] strExtention)
{
string ExcType = ex.GetType().FullName;
string ExcMsg = ex.Message;
string ExcLocation;
try
{
ExcLocation = GetExceptionLocation(ex);
}
catch(Exception e)
{
throw e;
}
if(ExcLocation.Length > 3)
{
ExcLocation = ExcLocation.Substring(3);
}
string strLogID = "";
strLogID = ExLogInfoFacade.GetLogInfoSequence();
try
{
ExLogInfoFacade.InsertLogInfo(strLogID, strLayerName, ExcType, ExcMsg, ExcLocation, strUserId, strExtention);
}
catch(Exception e)
{
throw e;
}
return Convert.ToInt32(strLogID);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -