gpt4 book ai didi

c# - 如何读取应用程序日志并根据 ASP.NET 中的源对其进行过滤?

转载 作者:行者123 更新时间:2023-11-30 18:05:19 24 4
gpt4 key购买 nike

在我的应用程序中,我想读取本地系统的应用程序事件日志。目前我正在使用以下代码

public partial class AppLog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
System.Diagnostics.EventLog logInfo = new System.Diagnostics.EventLog();
logInfo.Log = "Application";
logInfo.MachineName = "."; // Local machine
string strImage = ""; // Icon for the event
Response.Write("<p>There are " + logInfo.Entries.Count + " entries in the System event log.</p>");


foreach (EventLogEntry entry in logInfo.Entries.Cast<EventLogEntry>().Reverse<EventLogEntry>())
{
switch (entry.EntryType)
{
case EventLogEntryType.Warning:
strImage = "images/icon_warning.PNG";
break;
case EventLogEntryType.Error:
strImage = "images/icon_error.PNG";
break;
default:
strImage = "images/icon_info.PNG";
break;
}
Response.Write("<img src=\"" + strImage + "\">&nbsp;|&nbsp;");
Response.Write(entry.TimeGenerated.ToString() + "&nbsp;|&nbsp;");
Response.Write(entry.Message.ToString() + "<br>\r\n");
}
}
}
}

我只想显示由 ASP.NET 创建的日志。我知道我可以在 for-each 循环中过滤它。但这需要很多时间,因为它需要遍历整个应用程序日志。有什么办法吗在它进入任何迭代之前过滤它??

====编辑====

我有一个查询的方法,不知道是否真的提高了性能

    var result = (from EventLogEntry elog in logInfo.Entries
where (elog.Source.ToString().Equals("ASP.NET 2.0.50727.0"))
orderby elog.TimeGenerated descending
select elog).ToList();

并遍历结果列表。

最佳答案

var eventLog = new EventLog("Application");
eventLog.Entries
.Cast<EventLogEntry>()
.Select(e => e.Source == "yourFilter");

关于c# - 如何读取应用程序日志并根据 ASP.NET 中的源对其进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5711586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com