| | |
| | | { |
| | | public class Logger |
| | | { |
| | | |
| | | public static void Debug(string message) |
| | | /// <summary> |
| | | /// åå
¥è°è¯æ¥å¿ |
| | | /// </summary> |
| | | /// <param name="fileName"></param> |
| | | /// <param name="msg"></param> |
| | | public void WriteDebug(string fileName, string msg) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Debug(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Info(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Info(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Warn(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Warning(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Error(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Error(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Fatal(string message) |
| | | { |
| | | |
| | | } |
| | | |
| | | public static void Fatal(string message, Exception exception) |
| | | { |
| | | |
| | | } |
| | | public static void Write_Log(string groupName, string logName, string content, object data = null) |
| | | { |
| | | DateTime nowTime = DateTime.Now; |
| | | string basePath = System.Environment.CurrentDirectory + "/Log/" + $"/{groupName}/{nowTime.ToString("yyyy-MM-dd")}"; |
| | | //妿æ¥å¿æä»¶ç®å½ä¸åå¨,åå建 |
| | | if (!Directory.Exists(basePath)) |
| | | if (AppSettings.Get(new string[] { "LogDeubgEnable" }).ObjToBool()) |
| | | { |
| | | Directory.CreateDirectory(basePath); |
| | | StringBuilder builder = new StringBuilder(msg); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append(Environment.NewLine); |
| | | WriteLogToFile($"Debug_{fileName}", builder.ToString()); |
| | | } |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åå
¥ä¿¡æ¯æ¥å¿ |
| | | /// </summary> |
| | | /// <param name="fileName"></param> |
| | | /// <param name="msg"></param> |
| | | public void WriteInfo(string fileName, string msg) |
| | | { |
| | | StringBuilder builder = new StringBuilder(msg); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append(Environment.NewLine); |
| | | WriteLogToFile($"Info_{fileName}", builder.ToString()); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åå
¥é误æ¥å¿ |
| | | /// </summary> |
| | | /// <param name="fileName"></param> |
| | | /// <param name="msg"></param> |
| | | /// <param name="ex"></param> |
| | | public void WriteError(string fileName, string msg, Exception ex) |
| | | { |
| | | StringBuilder builder = new StringBuilder(msg); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("é误信æ¯ï¼"); |
| | | builder.Append(ex.Message); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("é误æºï¼"); |
| | | builder.Append(ex.Source); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("éè¯¯å æ ï¼"); |
| | | builder.Append(ex.StackTrace); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("é误类åï¼"); |
| | | builder.Append(ex.GetType().ToString()); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append("éè¯¯æ¹æ³ï¼"); |
| | | builder.Append(ex.TargetSite?.ToString()); |
| | | builder.Append(Environment.NewLine); |
| | | builder.Append(Environment.NewLine); |
| | | WriteLogToFile($"Error_{fileName}", builder.ToString()); |
| | | } |
| | | |
| | | |
| | | ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); |
| | | string folderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"Log\\{DateTime.Now.ToString("yyyy-MM-dd")}"); |
| | | |
| | | private void WriteLogToFile(string fileName, string log) |
| | | { |
| | | try |
| | | { |
| | | FileStream fs = new FileStream(basePath + "/" + logName + $"{nowTime.ToString("yyMMdd")}.txt", FileMode.Append); |
| | | StreamWriter strwriter = new StreamWriter(fs); |
| | | try |
| | | // è¿å
¥åé |
| | | LogWriteLock.EnterWriteLock(); |
| | | |
| | | // 妿æä»¶å¤¹ä¸åå¨ï¼åå建æä»¶å¤¹ |
| | | if (!Directory.Exists(folderPath)) |
| | | { |
| | | strwriter.WriteLine(nowTime.ToString() + "." + nowTime.Millisecond); |
| | | strwriter.WriteLine(content); |
| | | if (data != null) |
| | | { |
| | | strwriter.WriteLine(JsonConvert.SerializeObject(data)); |
| | | } |
| | | strwriter.WriteLine("-------------------------------"); |
| | | strwriter.WriteLine(); |
| | | strwriter.Flush(); |
| | | Directory.CreateDirectory(folderPath); |
| | | } |
| | | catch { } |
| | | finally |
| | | { |
| | | strwriter.Close(); |
| | | fs.Close(); |
| | | } |
| | | // è·åæ¥å¿æä»¶è·¯å¾ |
| | | string logFilePath = Path.Combine(folderPath, GetLastAccessFileName(fileName)); |
| | | // è·åå½åæ¶é´ |
| | | DateTime now = DateTime.Now; |
| | | // æé æ¥å¿å
容 |
| | | string logContent = $"ã{now}ã{Environment.NewLine}{log}"; |
| | | |
| | | // å°æ¥å¿å
容追å å°æ¥å¿æä»¶ä¸ |
| | | File.AppendAllText(logFilePath, logContent); |
| | | } |
| | | catch { } |
| | | finally |
| | | { |
| | | // éåºåé |
| | | LogWriteLock.ExitWriteLock(); |
| | | } |
| | | } |
| | | |
| | | public static void WriteApiLog2DB(HttpContext context, string requestParameter, DateTime beginDate, string responseParameter, DateTime endDate, LoggerStatus loggerStatus) |
| | | int size = 10 * 1024 * 1024; |
| | | string ext = ".log"; |
| | | private string GetLogFilePath(string folderPath, string fileName) |
| | | { |
| | | try |
| | | // è·åæå®æä»¶å¤¹ä¸çæææä»¶ |
| | | var allFiles = new DirectoryInfo(folderPath); |
| | | // è·åç¬¦åæ¡ä»¶çæä»¶ï¼ææä»¶åéåºæå |
| | | var selectFiles = allFiles.GetFiles().Where(fi => fi.Name.ToLower().Contains(fileName.ToLower()) && fi.Extension.ToLower() == ext.ToLower() && fi.Length < size).OrderByDescending(d => d.Name).ToList(); |
| | | |
| | | FileInfo? file = selectFiles.FirstOrDefault(); |
| | | // 妿æç¬¦åæ¡ä»¶çæä»¶ï¼è¿å第ä¸ä¸ªæä»¶ç宿´è·¯å¾ |
| | | if (file != null) |
| | | { |
| | | if (context.Request.Method == "OPTIONS") return; |
| | | |
| | | if (context == null) |
| | | { |
| | | Console.WriteLine($"æªè·åå°httpcontextä¿¡æ¯,reqParam:{requestParameter},respParam:{responseParameter},success:{loggerStatus}"); |
| | | return; |
| | | } |
| | | |
| | | Dictionary<string, object> dic = new Dictionary<string, object> |
| | | { |
| | | {"BeginDate",beginDate }, |
| | | {"ElapsedTime",(endDate - beginDate).TotalMilliseconds.DoubleToInt() }, |
| | | {"EndDate",endDate }, |
| | | {"RequestParam",requestParameter }, |
| | | {"ResponseParam",responseParameter }, |
| | | {"Success",1 }, |
| | | {"Url",context.Request.Path.Value??"" }, |
| | | {"UserIP",context.GetUserIp() }, |
| | | {"UserName","App.User?.Name" }, |
| | | {"User_Id","0" } |
| | | |
| | | //{"BeginDate",beginDate }, |
| | | //{"ElapsedTime",(endDate - beginDate).TotalMilliseconds.ObjToInt() }, |
| | | //{"EndDate",endDate }, |
| | | //{"RequestParam",requestParameter }, |
| | | //{"ResponseParam",responseParameter }, |
| | | //{"Success",1 }, |
| | | //{"Url",context.Request.Path.Value??"" }, |
| | | //{"UserIP",context.GetUserIp() }, |
| | | //{"UserName","App.User?.Name" }, |
| | | //{"User_Id","App.User?.ID" } |
| | | }; |
| | | |
| | | |
| | | SqlSugarClient sqlSugarClient = DBContext.GetCustomDB(DBContext.GetConnectionConfig()); |
| | | sqlSugarClient.Insertable(dic).AS("Sys_Log").ExecuteCommand(); |
| | | return file.FullName; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | |
| | | } |
| | | // å¦ææ²¡æç¬¦åæ¡ä»¶çæä»¶ï¼è¿åä¸ä¸ªæ°çæä»¶è·¯å¾ï¼æä»¶å为åæä»¶åå ä¸å½åæ¶é´ |
| | | return Path.Combine(folderPath, $@"{fileName}_{DateTime.Now.ToString("HH-mm-ss")}.log"); |
| | | } |
| | | } |
| | | |
| | | public enum LoggerStatus |
| | | { |
| | | Success = 1, |
| | | Error = 2, |
| | | Info = 3 |
| | | private string GetLastAccessFileName(string fileName) |
| | | { |
| | | foreach (var m in GetExistLogFileNames(fileName)) |
| | | { |
| | | FileInfo fileInfo = new FileInfo(m); |
| | | if (fileInfo.Length < size) |
| | | { |
| | | return m; |
| | | } |
| | | } |
| | | |
| | | // è¿åä¸ä¸ªæ°çé»è®¤å½åæ¶é´çæ¥å¿åç§° |
| | | return $@"{fileName}_{DateTime.Now.ToString("HH-mm-ss")}.log"; |
| | | } |
| | | |
| | | private string[] GetExistLogFileNames(string fileName) |
| | | { |
| | | string[] fileNames = Directory.GetFiles(folderPath, fileName + "*.log"); |
| | | return fileNames; |
| | | } |
| | | } |
| | | } |