【发布时间】: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