.NET医院公众号系统线程CPU双高问题分析(2)

所以得优化一下 NLog 的默认配置,貌似这样就可以结束本文了,不行,既然都到这里了,我还得找点开发人员责任😁😁😁。

3. 如何找开发人员责任

如果你细心的话,会不会觉得还漏了点什么? 对,就是那个同步块,卦象上有三条信息,对吧,为了方便查看,我再赘贴一下。

0:000> !syncblk Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner 95 00000262b8a30ca8 193 1 00000262b8a36b50 116b8 53 0000025e2a8ded70 System.Object 118 00000262b8a32098 107 1 00000262bad503b0 710c 135 00000260ea8a9b00 NLog.Logger 200 00000262ba236cc8 13 1 00000262b9df1660 8858 69 0000025e2a8dcdf0 System.Object

index=95 和 NLoger 相关,那怎么 index=118 又和 NLog.Logger 相关呢?接下来把这两个对象 0000025e2a8ded70, 00000260ea8a9b00 的源码导出来,可以用 !gcroot + !name2ee + !savemodule。

0:053> !gcroot 0000025e2a8ded70 Thread 116b8: 0000006d65d3d590 00007ff7d2a487b7 NLog.Targets.Target.WriteAsyncLogEvent(NLog.Common.AsyncLogEventInfo) rbp-48: 0000006d65d3d5b8 -> 0000025e2a8ded70 System.Object 0:053> !name2ee *!NLog.Targets.Target.WriteAsyncLogEvent -------------------------------------- Module: 00007ff7d2b172d8 Assembly: NLog.dll Token: 0000000006000b5e MethodDesc: 00007ff7d2be3330 Name: NLog.Targets.Target.WriteAsyncLogEvent(NLog.Common.AsyncLogEventInfo) JITTED Code Address: 00007ff7d2a48700 -------------------------------------- 0:053> !savemodule 00007ff7d2b172d8 E:\dumps\1.dll 3 sections in file section 0 - VA=2000, VASize=7faa4, FileAddr=200, FileSize=7fc00 section 1 - VA=82000, VASize=3e8, FileAddr=7fe00, FileSize=400 section 2 - VA=84000, VASize=c, FileAddr=80200, FileSize=200 0:053> !gcroot 00000260ea8a9b00 Thread 710c: 0000006d68f3df30 00007ff7d2d8a3b2 xxx.Logger.log(System.String) rdi: -> 00000260ea8a9b00 NLog.Logger 0:053> !name2ee *!xxx.Logger.log -------------------------------------- Module: 00007ff7d29b5558 Assembly: xxx.dll Token: 0000000006001ead MethodDesc: 00007ff7d29b9a38 Name: xxx.Logger.log(System.String) JITTED Code Address: 00007ff7d2d8a260 -------------------------------------- 0:053> !savemodule 00007ff7d29b5558 E:\dumps\2.dll 3 sections in file section 0 - VA=2000, VASize=221cf0, FileAddr=200, FileSize=221e00 section 1 - VA=224000, VASize=3c8, FileAddr=222000, FileSize=400 section 2 - VA=226000, VASize=c, FileAddr=222400, FileSize=200

用 ILSpy 打开 2.dll 后,发现了那段有趣的 Logger.log() 代码,真的是太有趣了。。。如下所示。

public class Logger { private static Logger Log = LogManager.GetLogger(""); private static object lockCache = new object(); public static void WriteLog(string message) { Task.Run(delegate { log(message); }); } public static void log(string message) { try { if (message.Contains("xxxxxxx")) { lock (Log) { Log.Warn(" " + message + "\r\n\r\n"); } } else { lock (Log) { Log.Info(" " + message + "\r\n\r\n"); } } } catch (Exception) { } } public static void WriteLog(string message, params object[] args) { lock (Log) { Log.Info(" " + string.Format(message, args)); } } }

居然在 log() 方法里加了一个锁,这是有多么不信任 NLog 哈 😂😂😂,还有一点在 WriteLog() 方法中使用了 Task.Run 记录日志,难怪朋友说分分钟上千个线程,这回我可是明白了。。。

当我以为就这样吐吐槽就结束了,不争气的我又看了另外一个 dump ,然后我就不想吐槽了😤

0:000> !t ThreadCount: 200 UnstartedThread: 0 BackgroundThread: 200 PendingThread: 0 DeadThread: 0 Hosted Runtime: no Lock ID OSID ThreadOBJ State GC Mode GC Alloc Context Domain Count Apt Exception 78 47 afb8 000001cd7abbf1d0 3029220 Preemptive 000001CBB81648C0:000001CBB8166318 000001cd798a9d30 2 MTA (Threadpool Worker) System.IO.FileLoadException 000001cbb81644a8 0:000> !PrintException /d 000001cbb81644a8 Exception object: 000001cbb81644a8 Exception type: System.IO.FileLoadException Message: 另一个程序正在使用此文件,进程无法访问。 (异常来自 HRESULT:0x80070020) InnerException: <none> StackTrace (generated): SP IP Function 0000001B3703E750 0000000000000000 mscorlib_ni!System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32, IntPtr)+0x1 0000001B3703E750 00007FF7D2D30D87 UNKNOWN!NLog.Internal.FileAppenders.BaseFileAppender.WindowsCreateFile(System.String, Boolean)+0x157 0000001B3703E7D0 00007FF7D2D3092F UNKNOWN!NLog.Internal.FileAppenders.BaseFileAppender.TryCreateFileStream(Boolean)+0x5f 0000001B3703E830 00007FF7D2D30593 UNKNOWN!NLog.Internal.FileAppenders.BaseFileAppender.CreateFileStream(Boolean)+0xd3 StackTraceString: <none> HResult: 80070020 The current thread is unmanaged

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyggfs.html