📄 applog.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Configuration;
namespace Qeb.Support.Common
{
/// <summary>
/// 日志记录
/// </summary>
public class AppLog
{
public static void Add(AppError error)
{
string lc = "\r\n";
StringBuilder sb = new StringBuilder();
sb.Append("发生时间:" + error.ErrorTime.ToString("yyyy-MM-dd HH:mm:ss") + lc);
sb.Append("错误信息:" + error.Message + lc);
sb.Append("详细信息:" + error.Description + lc);
if (error.Source != null)
{
//错误来源
if (error.Source == null)
sb.Append("错误来源:" + "null" + lc);
else
sb.Append("错误来源:" + error.Source.ToString() + lc);
//所在方法
if (error.TargetSite == null)
sb.Append("所在方法:" + "null" + lc);
else
sb.Append("所在方法:" + error.TargetSite.Name + lc);
//堆栈信息
if (error.StackTrace == null)
sb.Append("堆栈信息:" + "null" + lc);
else
sb.Append("堆栈信息:" + error.StackTrace + lc);
}
else
{
//错误来源
if (error.GetBaseException().Source == null)
sb.Append("错误来源:" + "null" + lc);
else
sb.Append("错误来源:" + error.GetBaseException().Source.ToString() + lc);
//所在方法
if (error.GetBaseException().TargetSite == null)
sb.Append("所在方法:" + "null" + lc);
else
sb.Append("所在方法:" + error.GetBaseException().TargetSite.Name + lc);
//堆栈信息
if (error.GetBaseException().StackTrace == null)
sb.Append("堆栈信息:" + "null" + lc);
else
sb.Append("堆栈信息:" + error.GetBaseException().StackTrace + lc);
}
try
{
Add(sb.ToString(), LogEntryType.Error, error.AssemblyName, error.TypeName);
}
catch (Exception ex)
{
System.Diagnostics.Trace.Write("记录日志时发生错误,该错误已被忽略");
System.Diagnostics.Trace.Write("错误信息:" + ex.ToString());
}
}
public static void Add(string message, LogEntryType logEntryType, Type objType)
{
string lc = "\r\n";
StringBuilder sb = new StringBuilder();
sb.Append("发生时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + lc);
sb.Append("详细信息:" + message + lc);
try
{
Add(sb.ToString(), logEntryType, objType.Assembly.FullName, objType.FullName);
}
catch(Exception ex)
{
System.Diagnostics.Trace.Write("记录日志时发生错误,该错误已被忽略");
System.Diagnostics.Trace.Write("错误信息:"+ex.ToString());
}
}
private static void Add(string message, LogEntryType logEntryType, string assemblyName, string typeName)
{
//机器名称
//IP
//UserID
//UserName
//GUID
string logType = "";
switch (logEntryType)
{
case LogEntryType.Audit:
logType = "【审计】";
break;
case LogEntryType.Debug:
logType = "【调试】";
break;
case LogEntryType.Error:
logType = "【错误】";
break;
case LogEntryType.Info:
logType = "【信息】";
break;
case LogEntryType.Warning:
logType = "【警告】";
break;
default:
logType = "【未定义信息类型】";
break;
}
WriteFileLog(logType + "\r\n");
WriteFileLog(message+"\r\n");
WriteFileLog(assemblyName+"\r\n");
WriteFileLog(typeName + "\r\n");
WriteFileLog("\r\n\r\n\r\n");
}
// 向文件写日志
private static void WriteFileLog(string message)
{
byte[] fileContent = Encoding.UTF8.GetBytes(message);
FileStream fs = new FileStream(@"d:\temp\mylog.log", FileMode.Append, FileAccess.Write, FileShare.None, 2048, true);
try
{
AutoResetEvent manualEvent = new AutoResetEvent(false);
IAsyncResult asyncResult = fs.BeginWrite(fileContent, 0, fileContent.Length,
new AsyncCallback(EndWriteCallback),
new WriteState(fs, manualEvent));
//manualEvent.WaitOne(3000, false);
}
catch (Exception ex)
{
throw ex;
}
finally
{
fs.Close();
}
}
// 异步写
private static void EndWriteCallback(IAsyncResult asyncResult)
{
WriteState stateInfo = (WriteState)asyncResult.AsyncState;
int workerThreads;
int portThreads;
try
{
ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
stateInfo.fStream.EndWrite(asyncResult);
//Thread.Sleep(1000);
}
finally
{
stateInfo.autoEvent.Set();
}
}
}
/// <summary>
/// 异步请求区别类
/// </summary>
internal sealed class WriteState
{
/// <summary>
/// 文件流
/// </summary>
public FileStream fStream;
/// <summary>
/// 事件
/// </summary>
public AutoResetEvent autoEvent;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="fStream">文件流</param>
/// <param name="autoEvent">事件</param>
public WriteState(FileStream fStream, AutoResetEvent autoEvent)
{
this.fStream = fStream;
this.autoEvent = autoEvent;
}
}
public enum LogEntryType
{
/// <summary>
/// 错误
/// </summary>
Error,
/// <summary>
/// 警告
/// </summary>
Warning,
/// <summary>
/// 一般信息
/// </summary>
Info,
/// <summary>
/// 调试信息
/// </summary>
Debug,
/// <summary>
/// 审计跟踪(某项业务成功,记录以备查)
/// </summary>
Audit
}
/*BAL,
DAL,
UI,*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -