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

标签 c# asp.net events

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

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/

相关文章:

asp.net - 将参数传递到 ASP .NET 页面

jquery - 无法捕获在 jQuery 中按下的删除键

javascript - JQuery,表格单元格上的事件冒泡。如何计算出点击了哪一行

javascript - 如何判断网页是通过 Flash 退出/卸载还是通过普通 HTML 退出/卸载

c# - 使用 BinaryPrimitives 填充字节缓冲区的首选方法?

c# - ASP.NET Identity 2.0 Roles.IsUserInRole 给出 {"Invalid object name ' dbo.aspnet_SchemaVersions'."} 异常

c# - 为什么同样的 msdn C# 示例程序输出与我的不一样?

c# - 如何避免双列(预定义列+ selectcommand)?

javascript - 页面加载期间 IE 中没有图像的高度和宽度

c# - 在调用 DateTime.AddMonths(int months) 之前我可以做安全检查吗?