【问题标题】:Debugging Program Keeps Sending Emails After Exit调试程序退出后继续发送电子邮件
【发布时间】:2017-01-30 19:03:25
【问题描述】:

我将以下代码用作读取一些事件日志的程序的一部分。

今天早上单步执行代码以测试某些内容后,我不断收到通过电子邮件发送给我的错误消息:

QueryEvents 中发生 EventLogHelper 错误

异常:RPC 服务器不可用

用户:

客户:D7-089

该进程没有在我的机器上运行,我只执行过一次或两次有问题的方法。然而,消息不断出现。每条消息之间的时间间隔似乎有所不同,我现在至少收到了 15 条。

这怎么可能?我觉得如果程序正在向我发送电子邮件,它一定是在某处执行,但我在Task ManagerProcesses 选项卡中看不到它,我尝试结束所有Visual Studio 相关进程,没有任何乐趣。


我重新启动了运行 VS 的电脑,但我仍然收到电子邮件。


Main()

public static void Main()
{
    var eh = new EventLogHelper();
    var eventFired = eh.CheckEvents();
}

EventLogHelper.cs

public readonly string PcName;
private readonly int Timespan;
private readonly string Filter;
private static EventLogSession Session;

/// <summary>
/// ctor
/// </summary>
public EventLogHelper()
{
    Timespan = 30000; // 30 seconds
    PcName = "D7-089"; // This is usually "Environment.MachineName", but specified it here for testing
    Filter = $"*[System[(EventID='5061' or EventID='5058') and TimeCreated[timediff(@SystemTime) <= {Timespan}]]]";
}

检查事件

/// <summary>
/// Checks the event logs for remote pc and returns true if any of the events we are interested in fired
/// </summary>
/// <returns></returns>
public bool CheckEvents()
{
    var query = BuildQuery(PcName, Filter);
    
    for (var i = 0; i < 60; i++)
    {
        var logs = QueryEvents(query);
        var events = ReadLogs(logs);

        if (events > 0)
        {
            return true;
        }

        System.Threading.Thread.Sleep(1000);
    }

    return false;
}

构建查询

/// <summary>
/// Builds an EventLogQuery for the given pcname and filter. This should be set up with a user who has admin rights
/// </summary>bh
private static EventLogQuery BuildQuery(string pcName, string filter)
{
    try
    {
        using (var pw = GetPassword())
        {
            Session = new EventLogSession(
            pcName,
            "DOMAIN",
            "USER",
            pw,
            SessionAuthentication.Default);
        }

        return new EventLogQuery("Security", PathType.LogName, filter)
            { Session = Session };
    }
    catch (Exception ex)
    {
        Email.Send($"EventLogHelper error occurred in BuildQuery \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {pcName}");
        Environment.Exit(Environment.ExitCode);
        return null;
    }
}

查询事件

这是发生错误的地方。我通过这种方法最多执行了 2 次,当我输入这个问题时,我仍然收到错误电子邮件。

/// <summary>
/// Execute the given EventLogQuery
/// </summary>
private EventLogReader QueryEvents(EventLogQuery query)
{
    try
    {
        return new EventLogReader(query);
    }
    catch (Exception ex)
    {
        Email.Send($"EventLogHelper error occurred in QueryEvents \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {PcName}");
        Environment.Exit(Environment.ExitCode);
        return null;
    }
}

读取日志

/// <summary>
/// Read the given EventLogReader and return the amount of events that match the IDs we are looking for
/// </summary>
/// <param name="logReader"></param>
/// <returns></returns>
private int ReadLogs(EventLogReader logReader)
{
    var count5058 = 0;
    var count5061 = 0;
    EventRecord entry;

    try
    {
        while ((entry = logReader.ReadEvent()) != null)
        {

            if (entry.Id == 5058)
            {
                count5058++;
            }
            else
            {
                count5061++;
            }
        }
    }
    catch (Exception ex)
    {
        Email.Send($"EventLogHelper error occurred in ReadLogs \n\n Exception: {ex.Message} \n\n User: {Program.UserName} \n\n Client: {PcName}");
        Environment.Exit(Environment.ExitCode);
    }

    return count5058 + count5061;
}

【问题讨论】:

    标签: c# visual-studio debugging events error-handling


    【解决方案1】:

    原来这是我的一个愚蠢的错误造成的。

    这个程序有一个构建后事件,它被调用:

    if $(ConfigurationName) == Release ("$(ProjectDir)PostBuildRelease.bat" "$(TargetDir)" @(VersionNumber) "$(TargetFileName)" "$(TargetName)")
    

    所以它只在 VS 构建配置设置为 Release 时运行。

    PostBuildRelease.bat 只需将生成的程序集复制到活动位置,以便用户在登录时复制到他们的桌面。

    • 在测试我的应用程序时,我愚蠢地编辑了源代码以查询特定的 PC,然后单步执行代码。

    • 1234563
    • 如果代码使用硬编码的PcName 运行,而该 PC 不是当前计算机,则事件查询似乎失败并显示上述错误消息。

    • 所以我收到的所有电子邮件都已发送出去,因为该程序实际上是在用户 PC 上执行的。但是因为PcName 是硬编码的,所以它看起来总是来自我的程序实例!

    这里的教训是始终注意当前选择了哪个构建配置,尤其是在指定了构建后事件的情况下。

    【讨论】:

    • 既然这个问题已经解决,请将其标记为答案,这样对遇到相同问题的其他社区成员有帮助,谢谢分享:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-08
    • 2016-01-14
    • 1970-01-01
    • 2018-05-10
    • 2015-06-07
    • 1970-01-01
    • 2016-03-25
    相关资源
    最近更新 更多