¶Ô±ÈÐÂÎļþ |
| | |
| | |  |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Helper; |
| | | |
| | | namespace WIDESEA_Core.LogHelper |
| | | { |
| | | public class LogLock |
| | | { |
| | | static ReaderWriterLockSlim LogWriteLock = new ReaderWriterLockSlim(); |
| | | static int WritedCount = 0; |
| | | static int FailedCount = 0; |
| | | static string _contentRoot = string.Empty; |
| | | |
| | | public LogLock(string contentPath) |
| | | { |
| | | _contentRoot = contentPath; |
| | | } |
| | | |
| | | public static void OutLogAOP(string prefix, string[] dataParas, bool IsHeader = true) |
| | | { |
| | | OutSql2LogToFile(prefix, dataParas, IsHeader); |
| | | } |
| | | |
| | | public static void OutSql2LogToFile(string prefix, string[] dataParas, bool IsHeader = true, bool isWrt = false) |
| | | { |
| | | try |
| | | { |
| | | //设置读åé为åå
¥æ¨¡å¼ç¬å èµæºï¼å
¶ä»åå
¥è¯·æ±éè¦çå¾
æ¬æ¬¡åå
¥ç»æä¹åæè½ç»§ç»åå
¥ |
| | | //注æï¼é¿æ¶é´ææè¯»çº¿ç¨éæå线ç¨éä¼ä½¿å
¶ä»çº¿ç¨åç饥饿 (starve)ã 为äºå¾å°æå¥½çæ§è½ï¼éè¦èèéæ°æé åºç¨ç¨åºä»¥å°å访é®çæç»æ¶é´åå°å°æå°ã |
| | | // 仿§è½æ¹é¢èèï¼è¯·æ±è¿å
¥åå
¥æ¨¡å¼åºè¯¥ç´§è·æä»¶æä½ä¹åï¼å¨æ¤å¤è¿å
¥åå
¥æ¨¡å¼ä»
æ¯ä¸ºäºéä½ä»£ç å¤æåº¦ |
| | | // å è¿å
¥ä¸éåºåå
¥æ¨¡å¼åºå¨åä¸ä¸ªtry finallyè¯å¥åå
ï¼æä»¥å¨è¯·æ±è¿å
¥åå
¥æ¨¡å¼ä¹åä¸è½è§¦åå¼å¸¸ï¼å¦åéæ¾æ¬¡æ°å¤§äºè¯·æ±æ¬¡æ°å°ä¼è§¦åå¼å¸¸ |
| | | LogWriteLock.EnterWriteLock(); |
| | | |
| | | var folderPath = Path.Combine(_contentRoot, "Log"); |
| | | if (!Directory.Exists(folderPath)) |
| | | { |
| | | Directory.CreateDirectory(folderPath); |
| | | } |
| | | //string logFilePath = Path.Combine(path, $@"{filename}.log"); |
| | | var logFilePath = FileHelper.GetAvailableFileWithPrefixOrderSize(folderPath, prefix); |
| | | |
| | | var now = DateTime.Now; |
| | | string logContent = String.Join("\r\n", dataParas); |
| | | if (IsHeader) |
| | | { |
| | | logContent = ( |
| | | "--------------------------------\r\n" + |
| | | DateTime.Now + "|\r\n" + |
| | | String.Join("\r\n", dataParas) + "\r\n" |
| | | ); |
| | | } |
| | | else |
| | | { |
| | | logContent = ( |
| | | dataParas[1] + ",\r\n" |
| | | ); |
| | | } |
| | | |
| | | //if (logContent.IsNotEmptyOrNull() && logContent.Length > 500) |
| | | //{ |
| | | // logContent = logContent.Substring(0, 500) + "\r\n"; |
| | | //} |
| | | if (isWrt) |
| | | { |
| | | System.IO.File.WriteAllText(logFilePath, logContent); |
| | | } |
| | | else |
| | | { |
| | | System.IO.File.AppendAllText(logFilePath, logContent); |
| | | } |
| | | WritedCount++; |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | //Console.Write(e.Message); |
| | | FailedCount++; |
| | | } |
| | | finally |
| | | { |
| | | //éåºåå
¥æ¨¡å¼ï¼éæ¾èµæºå ç¨ |
| | | //注æï¼ä¸æ¬¡è¯·æ±å¯¹åºä¸æ¬¡éæ¾ |
| | | // è¥éæ¾æ¬¡æ°å¤§äºè¯·æ±æ¬¡æ°å°ä¼è§¦åå¼å¸¸[åå
¥é宿ªç»ä¿æå³è¢«éæ¾] |
| | | // è¥è¯·æ±å¤çå®æåæªéæ¾å°ä¼è§¦åå¼å¸¸[æ¤æ¨¡å¼ä¸ä¸å
许以é彿¹å¼è·ååå
¥éå®] |
| | | LogWriteLock.ExitWriteLock(); |
| | | } |
| | | } |
| | | } |
| | | } |