从事件日志中获取事件的详细信息

如何解决从事件日志中获取事件的详细信息

| 我试图基于列表框中项目的选择从事件日志中获取详细信息。我试图将详细信息放入文本框。我已经成功地找到了自己的解决方案。我所做的事情(而且非常慢)是在事件日志中重申并找到与日志索引的匹配项,然后显示消息,但这是一项耗时的操作。有没有一种更快的方法可以根据日志索引直接到达特定的日志条目。我正在使用WPF和C#。
private void backgroundWorker2_DoWork(object sender,DoWorkEventArgs e)
{
    EventLog eventLog1 = new EventLog();
    eventLog1.Log = \"System\";
    foreach (System.Diagnostics.EventLogEntry entry in eventLog1.Entries)
    {
        var newEntry = entry.Index + \"  -  \" + entry.EntryType + \" -           \" + entry.TimeWritten + \"     - \" + entry.Source;

        backgroundWorker2.ReportProgress(0,newEntry);
    }
}

void backgroundWorker2_ProgressChanged(object sender,ProgressChangedEventArgs e)
{
    var newEntry = (string)e.UserState;

   MainWindow.Instance.Dispatcher.BeginInvoke(new Action(delegate() { MainWindow.Instance.listBox1.Items.Add(newEntry); }));
}
然后将每个项目与该项目的索引一起添加到列表框中,然后浏览并提取索引:
private void listBox1_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
    string p1 = listBox1.SelectedItem.ToString();
    string[] id = Regex.Split(p1,@\"([\\s])\");
    label1.Content = id[0];
    EventLog el = new EventLog();
    el.Log = \"System\";
    foreach (System.Diagnostics.EventLogEntry entry in el.Entries)
    {
        if (entry.Index.ToString() == id[0])
        {
            label1.Content = entry.Message;
        }

    }
}
foreach循环是导致UI挂起的原因,但是即使我将其设置在其他线程上,就像我在将项目添加到列表框中时所做的那样,仍然需要一些时间来完成所有操作才能获得我正在寻找确切的索引。因此,Im真正想做的只是直接进入该索引并获取消息,而不是遍历整个列表进行搜索。     

解决方法

        查看您的代码后,以下是我的简要操作: 卸下
ProgressChanged
装卸器。它旨在向用户报告当前状态,而您不会这样做。而是在
DoWork
处理程序中调用
Items.Add
。 一次创建
EventLog
。这看起来似乎更好,并且如果您不小心的话,可以避免您可能在循环中创建它。 与其使用正则表达式解析文本,不如创建一个特殊的类。这确实很重要,当您需要更准确的行为或根本不希望显示索引时,将为您节省很多痛苦。正则表达式很慢,并且绝不能解析要显示给用户的数据。您必须使用类。 使用有意义的名称。我知道您没有清理代码,但是如果您希望有人通过Internet来帮助您,那么您确实应该这样做。 最后,按索引获取项目。如果查看文档,您会注意到有一个indexer属性,该属性直接通过其索引获取项目。
class EntryItem {
    public EntryItem (EventLogEntry entry) 
    {
        EntryIndex = entry.Index;
        ItemText = string.Format (\"{0} - {1} - {2}     - {3}\",entry.Index,entry.EntryType,entry.TimeWritten,entry.Source);
    }

    public string ItemText { get; private set; }
    public int EntryIndex { get; private set; }

    public override string ToString ()
    {
        return ItemText;
    }
}

private EventLog log = new EventLog {
    Log = \"System\"
};

private void eventLoader_DoWork (object sender,DoWorkEventArgs e)
{
    foreach (EventLogEntry entry in this.log.Entries)
        this.Dispatcher.BeginInvoke (() => eventListBox.Items.Add (new EntryItem (entry)));
}

private void eventListBox_SelectionChanged (object sender,SelectionChangedEventArgs e)
{
    EntryItem item = eventListBox.SelectedItem as EntryItem;
    if (item == null)
        return;

    var entry = log.Entries [item.EntryIndex];
    currentEntryLabel.Content = entry.Message;
}
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-