在MVC專案下建立Log類別
public class LogWriter<T> where T : Enum
{
/// <summary>路徑</summary>
private static string Path = "C:\\LogFile";
/// <summary>檔案名稱</summary>
private static string Name = "Log";
/// <summary>篩選內容</summary>
private static T AllowType;
/// <summary>
/// 設定路徑和名稱
/// </summary>
/// <param name="NewPath">路徑</param>
/// <param name="NewName">名稱</param>
/// <param name="NewAllowType">篩選的列舉</param>
public static void SetPathAndName(string NewPath, string NewName, T NewAllowType)
{
Path = NewPath;
Name = NewName;
AllowType = NewAllowType;
}
/// <summary>
/// 更換新的允許
/// </summary>
/// <param name="NewAllowType">新允許</param>
public void ChangeAllow(T NewAllowType)
{
AllowType = NewAllowType;
}
/// <summary>產生Log檔</summary>
public static void CreateLogger()
{
//如果此路徑沒有資料夾
if (!Directory.Exists(Path))
{
//新增資料夾
Directory.CreateDirectory(Path);
}
WriteLog("=======================================");
WriteLog("程式啟動 : " + DateTime.Now.ToString());
WriteLog("=======================================");
}
/// <summary>
/// 寫入Log
/// </summary>
/// <param name="log">Log內容</param>
public static void WriteLog(string log)
{
if (!Directory.Exists(Path))
CreateLogger();
using (StreamWriter sw = File.AppendText(Path + "\\" + Name))
sw.WriteLine(log);
}
/// <summary>
/// 寫入Log(複合列舉)
/// </summary>
/// <param name="LogType">Log類別</param>
/// <param name="ClassName">使用GetType的Name或FullName</param>
/// <param name="MethodName">使用MethodBase.GetCurrentMethod</param>
/// <param name="Message">Log內容</param>
/// <returns></returns>
public static void WriteLogs(T LogType, string ClassName, string MethodName, string Message)
{
if (!Directory.Exists(Path))
CreateLogger();
List<string> AllowTypeList = new List<string>();
foreach (T EnumType in Enum.GetValues(typeof(T)))
if (AllowType.HasFlag(EnumType) && LogType.HasFlag(EnumType))
AllowTypeList.Add(EnumType.ToString());
using (StreamWriter sw = File.AppendText(Path + "\\" + Name)){
sw.WriteLine($"[{string.Join("|", AllowTypeList)}] {ClassName}[{MethodName}] {Message}");
}
}
/// <summary>
/// 寫入Log(多列舉)
/// </summary>
/// <param name="LogType">Log類別</param>
/// <param name="ClassName">使用GetType的Name或FullName</param>
/// <param name="MethodName">使用MethodBase.GetCurrentMethod</param>
/// <param name="Message">Log內容</param>
/// <returns></returns>
public static void WriteLogm(T LogType, string ClassName, string MethodName, string Message)
{
if (!Directory.Exists(Path))
CreateLogger();
foreach (T EnumType in Enum.GetValues(typeof(T)))
if (LogType.HasFlag(EnumType))
WriteLog(EnumType, ClassName, MethodName, Message);
}
/// <summary>
/// 寫入Log
/// </summary>
/// <param name="LogType">Log類別</param>
/// <param name="ClassName">使用GetType的Name或FullName</param>
/// <param name="MethodName">使用MethodBase.GetCurrentMethod</param>
/// <param name="Message">Log內容</param>
/// <returns></returns>
public static void WriteLog(T LogType, string ClassName, string MethodName, string Message)
{
if (!AllowType.HasFlag(LogType))
return;
WriteLog($"[{LogType}] {ClassName}[{MethodName}] {Message}");
}
}
建立Log用Enum
public class ConstEnum
{
/// <summary>Log列舉</summary>
[Flags]
public enum ELogType
{
#region 介面
/// <summary>網站</summary>
WEB = 0x0001,
/// <summary>核心</summary>
CORE = 0x0002,
#endregion
#region 類型
/// <summary>訊息</summary>
INFO = 0x0004,
/// <summary>警告</summary>
WARN = 0x0008,
/// <summary>錯誤</summary>
ERROR = 0x0010,
#endregion
}
}
在應用程式開始和結束的地方加入
namespace WebProjectName
{
/// <summary>執行端</summary>
public class MvcApplication : System.Web.HttpApplication
{
/// <summary>應用程式開始</summary>
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
//加入路由
RouteConfig.RegisterRoutes(RouteTable.Routes);
//封裝JS和CSS檔
BundleConfig.RegisterBundles(BundleTable.Bundles);
//開啟log記錄
LogWriter<ELogType>.SetPathAndName("C:\\LogFile", "Log", (ELogType)0xFFFF);
LogWriter<ELogType>.CreateLogger();
}
/// <summary>應用程式結束</summary>
protected void Application_End()
{
LogWriter<ELogType>.WriteLog("============================");
LogWriter<ELogType>.WriteLog("程式結束Application_End : " + DateTime.Now.ToString());
LogWriter<ELogType>.WriteLog("============================");
}
/// <summary>程式出錯</summary>
protected void Application_Error()
{
Exception ErrorMsg = Server.GetLastError();
LogWriter<ELogType>.WriteLog($"[Application_Error] Exception:{ErrorMsg.GetType().Name}({ErrorMsg.Message}) \nSource:{ErrorMsg.Source} \nStackTrace:{ErrorMsg.StackTrace}");
}
}
}
使用方式
LogWriter.WriteLogs(
ELogType.WEB | ELogType.WARN,
GetType().Name,
MethodBase.GetCurrentMethod().Name, "訊息");