在我的应用程序中,我想读取本地系统的应用程序事件日志。 目前我正在使用以下代码
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 + "\"> | ");
Response.Write(entry.TimeGenerated.ToString() + " | ");
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/