【问题标题】:Logging to IIS log file from asmx service - C#从 asmx 服务记录到 IIS 日志文件 - C#
【发布时间】:2021-05-21 04:25:16
【问题描述】:

我正在开发一个 asmx 网络服务。我正在尝试使用 C# 以编程方式附加到 IIS 日志文件。 日志文件由 IIS 每天创建。在解决方案中安装了 Nuget 包 - Microsoft.Web.Administration - v7.0。 IIS 服务器版本 10。我尝试了以下两种方法。 请给我建议。任何帮助表示赞赏。谢谢你。 示例代码如下,

           var serverManager = new ServerManager();

           string path = string.Empty;
           string fullPath = string.Empty;
           string logFile = string.Empty;

           foreach (var site in serverManager.Sites) 
           {
               path = System.IO.Path.Combine(site.LogFile.Directory);
               DirectoryInfo directory = new DirectoryInfo(path);

               foreach (var dir in directory.GetDirectories()) 
               {
                   if (dir.ToString() == "W3SV") {
                       logFile = dir.GetFiles().OrderByDescending(f => f.Name).FirstOrDefault().ToString(); 
                   
                   }
               
               }
           }

           fullPath = path + @"\W3SV\" + logFile;

           try {

               TextWriter tw = File.CreateText(fullPath);
               HttpResponse response = new HttpResponse(tw);
               response.AppendToLog("Add to Log file");
               tw.Flush();
               tw.Close();
           }
           catch (Exception ex) { }```


**Another approach I tried,**

try{
Http.Context.Current.Response.AppendToLog("Append to Log");
}

【问题讨论】:

    标签: c# logging iis httpresponse asmx


    【解决方案1】:

    w3sv 文件夹中的文件用于记录 http 请求并归 IIS 所有。它们的目的是以标准化格式包含每个请求一行的日志,这对于通过 http 状态代码分布、每个请求花费的时间等分析应用程序运行状况的更通用的方法很有用。修改此文件以包含常规操作日志将混淆了许多现有工具和服务器管理员,我认为这是一个坏主意。

    您可能会受益于将更多特定于应用程序的日志记录到位于其他位置的文件中。如果您需要在请求和此其他文件中的日志消息之间建立连接,您可以引入将消息链接到请求的关联 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2011-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多