C# 如何处理重叠控制台。Writeline()';什么是被解雇事件?

C# 如何处理重叠控制台。Writeline()';什么是被解雇事件?,c#,events,console-application,overlapping,console.writeline,C#,Events,Console Application,Overlapping,Console.writeline,嘿,我正在为自己的windows服务器编写一个RDP Bruteforce保护程序,它甚至在更改RDP端口后受到攻击:/ 但我已经在登录控制台时遇到了麻烦,当几乎同时发生两次攻击时,控制台输出“重叠”如下: 显示我基本工作的简约代码: watcher.EventRecordWritten += new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead); public stati

嘿,我正在为自己的windows服务器编写一个RDP Bruteforce保护程序,它甚至在更改RDP端口后受到攻击:/

但我已经在登录控制台时遇到了麻烦,当几乎同时发生两次攻击时,控制台输出“重叠”如下:

显示我基本工作的简约代码:

watcher.EventRecordWritten +=
                new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead);

public static async void EventLogEventRead(object obj,
        EventRecordWrittenEventArgs arg)
    {            
        if (arg.EventRecord != null)
        {
            string IP = GetIPFromRecord(arg.EventRecord)

            var json = await new HttpClient().GetStringAsync("https://api.ipgeolocationapi.com/geolocate/" + IP);
            var jsonDeserialized = new JavaScriptSerializer().Deserialize<dynamic>(json);
            string country = jsonDeserialized["name"];


            Console.WriteLine("IP:\t" + IP);
            Console.WriteLine("Country:\t" + country);
        }
        else
        {
            Console.WriteLine("The event instance was null.");
        }
    }
watcher.eventRecordWrited+=
新的EventHandler(EventLogEventRead);
公共静态异步void EventLogEventRead(对象obj,
EventRecordWrittenEventTargets参数)
{            
if(arg.EventRecord!=null)
{
字符串IP=GetIPFromRecord(arg.EventRecord)
var json=等待新的HttpClient()。GetStringAsync(“https://api.ipgeolocationapi.com/geolocate/“+IP);
var jsonDeserialized=新的JavaScriptSerializer()。反序列化(json);
字符串country=jsondserialized[“name”];
控制台写入线(“IP:\t”+IP);
Console.WriteLine(“国家:\t”+国家);
}
其他的
{
WriteLine(“事件实例为null”);
}
}
完整代码(无错误消息类):


那么,解决这样一个问题最优雅的方法是什么呢?

它是重叠的,因为您对单个日志条目有多个调用
Console.WriteLine()
。您需要准备整个输出主体,并立即将其写入控制台

例如,改变

Console.WriteLine("IP:\t" + IP);
Console.WriteLine("Country:\t" + country);

或者更好地使用
StringBuilder

var builder = new StringBuilder();
builder.AppendLine($"IP:\t{IP}");
builder.AppendLine($"Country:\t{country}");
Console.WriteLine(builder.ToString());
我还建议使用专门的日志框架,例如NLog()。如上所述,您仍然需要立即写入条目,但它可以帮助您设置输出样式,并在将来需要时轻松添加其他目标(文件、网络等)。

您应该使用锁:

watcher.EventRecordWritten +=
                new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead);


private static readonly object myLock = new object();

public static async void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
{          

    if (arg.EventRecord != null)
    {
        string IP = GetIPFromRecord(arg.EventRecord)

        var json = await new HttpClient().GetStringAsync("https://api.ipgeolocationapi.com/geolocate/" + IP);
        var jsonDeserialized = new JavaScriptSerializer().Deserialize<dynamic>(json);
        string country = jsonDeserialized["name"];

        lock (myLock) {
            Console.WriteLine("IP:\t" + IP);
            Console.WriteLine("Country:\t" + country);
        }
    }
    else
    {   
        lock (myLock) {
            Console.WriteLine("The event instance was null.");
        }
    }

}
watcher.eventRecordWrited+=
新的EventHandler(EventLogEventRead);
私有静态只读对象myLock=new object();
公共静态异步void EventLogEventRead(对象obj,eventRecordWrittenEventTargets arg)
{          
if(arg.EventRecord!=null)
{
字符串IP=GetIPFromRecord(arg.EventRecord)
var json=等待新的HttpClient()。GetStringAsync(“https://api.ipgeolocationapi.com/geolocate/“+IP);
var jsonDeserialized=新的JavaScriptSerializer()。反序列化(json);
字符串country=jsondserialized[“name”];
锁(myLock){
控制台写入线(“IP:\t”+IP);
Console.WriteLine(“国家:\t”+国家);
}
}
其他的
{   
锁(myLock){
WriteLine(“事件实例为null”);
}
}
}

嘿,非常感谢。不幸的是,在我的完整代码中,我使用Console.Write将状态消息着色。所以我想我不能这样准备。我应该在简短的代码中说明这一点。感谢NLog的提示,我将尝试一下。那么你必须使用@Charles在回答中建议的锁。如果你担心速度的话,也可以买些免锁的。非常感谢。如果我理解正确,例如,如果ipgeolocation调用一个日志需要更长的时间,那么例如有3个日志,一个在41秒,一个在42秒,一个在43秒,这将不能确保正确的顺序?我发现了,但我不确定如何不仅记录重叠的部分then@Taki7o7如果要确保事件日志的顺序41、42、43正确,可以在整个EventLogEventRead函数周围设置锁(mylock)。我把它放在这里的方式只能确保Console.WriteLine语句始终保持在一起。非常感谢,我以前甚至不知道lock函数。现在似乎很好用。
watcher.EventRecordWritten +=
                new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead);


private static readonly object myLock = new object();

public static async void EventLogEventRead(object obj, EventRecordWrittenEventArgs arg)
{          

    if (arg.EventRecord != null)
    {
        string IP = GetIPFromRecord(arg.EventRecord)

        var json = await new HttpClient().GetStringAsync("https://api.ipgeolocationapi.com/geolocate/" + IP);
        var jsonDeserialized = new JavaScriptSerializer().Deserialize<dynamic>(json);
        string country = jsonDeserialized["name"];

        lock (myLock) {
            Console.WriteLine("IP:\t" + IP);
            Console.WriteLine("Country:\t" + country);
        }
    }
    else
    {   
        lock (myLock) {
            Console.WriteLine("The event instance was null.");
        }
    }

}