My Github

传统.NET 4.x应用容器化体验(6)

在Windows Container中,没有写日志的情况下,如何排查系统的异常信息?

1 关于Windows事件日志

在以往基于IIS部署ASP.NET应用程序时,如果没有写指定日志的情况下,我们往往会使用Window事件日志来查看一些错误信息。

虽然事件日志的可读性和易用性不够好,但是还是可以帮助我们查看一些问题。这不,我在公司测试环境部署了我们团队的老系统(大单体ASP.NET MVC项目)的Service项目做POC试点验证,跑了几个接口之后发现系统直接返回503错误。对于第一次在Windows Container上跑ASP.NET MVC应用的我来说,有点懵,在容器内部查看IIS Log也没有足够的信息,我能想到的,就是去看事件日志了。

2 Docker下查看事件日志

Step1. 首先进入ASP.NET MVC容器实例内部:

>docker exec -it <container_name> powershell

Step2. 获取最新的20个事件日志,获得对应日志的Index:

>Get-Eventlog -newest 20 application
Index Time          EntryType   Source                 InstanceID Message
      96 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      95 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      94 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      93 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      92 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      91 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      90 Jul 22 15:34  Error       Application Error            1000 Faulting application name: w3wp.exe, version: 1...
      89 Jul 22 15:34  Error       .NET Runtime                 1026 Application: w3wp.exe...
      88 Jul 22 15:34  Error       ASP.NET 4.0.30319.0    3221226797 An unhandled exception occurred and the process...
      87 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      86 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      85 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      84 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      83 Jul 22 15:34  Information Windows Error Rep...         1001 Fault bucket , type 0...
      ......

Step3. 找到出错的那几个index,通过下面的命令查看错误日志:

>(Get-Eventlog -index 89 application).message
An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/1/ROOT

Process ID: 7664

Exception: System.TypeInitializationException

Message: The type initializer for 'NewLife.Log.XTrace' threw an exception.

StackTrace:    at NewLife.Log.XTrace.WriteException(Exception ex)
   at NewLife.Threading.ThreadPoolX.<>c__DisplayClass2_0.<QueueUserWorkItem>b__0(Object s)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

InnerException: System.UnauthorizedAccessException

Message: Access to the path 'C:\inetpub\wwwroot\Config' is denied.

StackTrace:    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost
)
   at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
   at System.IO.PathHelper.EnsureDirectory(String path, Boolean isfile)
   ......

从错误日志中可以看到,Config目录访问不到,经过调查发现,原来已有系统的IIS目录下有一个手动拷贝进去的Config目录(正确做法应该将其作为解决方案的一部分内容始终输出到release目录),于是乎将其拷贝到容器目录下,重新打镜像和运行,问题解决,平稳运行!

3 总结

本文介绍了如何在Windows Container中通过事件日志排查ASP.NET应用程序的异常日志信息,虽然文章很短小,但希望对你有用。

对于传统.NET 4.x应用的容器化迁移,我们也还在探索,相信探索和实践的深入,我会分享更多相关的内容。

 

 

posted @ 2021-11-28 14:03  EdisonZhou  阅读(138)  评论(0编辑  收藏  举报