c#日志记录帮助类分享
public class loghelper
{
private static void info(string category, int priority, traceeventtype severity, string message)
{
idictionary<string, object> dic = new dictionary<string, object>();
dic.add("属性:", category);
dic.add("内容:", message);
icollection<string> coll = new list<string>();
coll.add("general");
logentry log = new logentry();
log.priority = priority;
log.severity = severity;
log.message = category;//"日志测试";
log.timestamp = datetime.now;
log.extendedproperties = dic;//记录额外的信息
log.categories = coll;//设置记录的日志类型
logger.write(log);
}
public static void debug(string message)
{
info("debug", 1, traceeventtype.information, message);
}
public static void debugformat(string format, params object[] args)
{
info("debug", 1, traceeventtype.information, string.format(format, args));
}
public static void trace(string message)
{
info("trace", 1, traceeventtype.information, message);
}
public static void traceformat(string format, params object[] args)
{
info("trace", 1, traceeventtype.information, string.format(format, args));
}
public static void error(string message)
{
info("error", 1, traceeventtype.error, message);
}
public static void errorformat(string format, params object[] args)
{
info("error", 1, traceeventtype.error, string.format(format, args));
}
public static void error(object obj, exception ex)
{
info("error", 1, traceeventtype.error, string.format("error info:{0},{1}", obj, ex.message));
}
//日志记录
public static void writelog(string errortitle, string properties, string content)
{
idictionary<string, object> dic = new dictionary<string, object>();
dic.add("属性:", properties);
dic.add("内容:", content);
icollection<string> coll = new list<string>();
coll.add("general");
logentry log = new logentry();
log.message = errortitle;//"日志测试";
log.timestamp = datetime.now;
log.extendedproperties = dic;//记录额外的信息
log.categories = coll;//设置记录的日志类型
logger.write(log);
}
}
用法
#region 根据jobno获取对应操作人员姓名 employee 表
/// <summary>
/// 根据jobno获取对应操作人员姓名
/// </summary>
/// <param name="jobno">jobno</param>
/// <returns></returns>
public static string getmanagernamebyjobno(string jobno)
{
string strsql = "select in_user from impgtbill where job_no=@jobno";
try
{
object temp = sqlhelper.instance("conn_gm")
.executescalar(strsql, new[] { new sqlparameter("@jobno", jobno) });
if (temp != null)
{
return temp.tostring();
}
return "";
}
catch (exception e)
{
loghelper.errorformat("ordertitle_dal.getmanagernamebyjobno:{0}", e.message);
return null;
}
}
#endregion
上一篇: 配置ssh免密登录