From fe4c6fbd4576af470d7b32e1c61d98aa9e3c4787 Mon Sep 17 00:00:00 2001 From: Waterball Date: Tue, 2 Feb 2021 09:46:56 +0000 Subject: [PATCH] Fix example logging --- samples/01_basic_ping_bot/Program.cs | 9 +-- samples/02_commands_framework/Program.cs | 10 ---- samples/03_sharded_client/Program.cs | 7 --- .../Services/CommandHandlingService.cs | 8 --- samples/idn/Program.cs | 55 ++----------------- 5 files changed, 6 insertions(+), 83 deletions(-) diff --git a/samples/01_basic_ping_bot/Program.cs b/samples/01_basic_ping_bot/Program.cs index 7fbe04993..874eeb23d 100644 --- a/samples/01_basic_ping_bot/Program.cs +++ b/samples/01_basic_ping_bot/Program.cs @@ -31,8 +31,7 @@ namespace _01_basic_ping_bot // It is recommended to Dispose of a client when you are finished // using it, at the end of your app's lifetime. _client = new DiscordSocketClient(); - - _client.Log += LogAsync; + _client.Ready += ReadyAsync; _client.MessageReceived += MessageReceivedAsync; } @@ -47,12 +46,6 @@ namespace _01_basic_ping_bot await Task.Delay(Timeout.Infinite); } - private Task LogAsync(LogMessage log) - { - Console.WriteLine(log.ToString()); - return Task.CompletedTask; - } - // The Ready event indicates that the client has opened a // connection and it is now safe to access the cache. private Task ReadyAsync() diff --git a/samples/02_commands_framework/Program.cs b/samples/02_commands_framework/Program.cs index 67cb87764..f66b1e441 100644 --- a/samples/02_commands_framework/Program.cs +++ b/samples/02_commands_framework/Program.cs @@ -35,9 +35,6 @@ namespace _02_commands_framework { var client = services.GetRequiredService(); - client.Log += LogAsync; - services.GetRequiredService().Log += LogAsync; - // Tokens should be considered secret data and never hard-coded. // We can read from the environment variable to avoid hardcoding. await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); @@ -50,13 +47,6 @@ namespace _02_commands_framework } } - private Task LogAsync(LogMessage log) - { - Console.WriteLine(log.ToString()); - - return Task.CompletedTask; - } - private ServiceProvider ConfigureServices() { return new ServiceCollection() diff --git a/samples/03_sharded_client/Program.cs b/samples/03_sharded_client/Program.cs index 753f400a1..a5bbea1f2 100644 --- a/samples/03_sharded_client/Program.cs +++ b/samples/03_sharded_client/Program.cs @@ -38,7 +38,6 @@ namespace _03_sharded_client // The ShardReady event is used instead, allowing for individual // control per shard. client.ShardReady += ReadyAsync; - client.Log += LogAsync; await services.GetRequiredService().InitializeAsync(); @@ -65,11 +64,5 @@ namespace _03_sharded_client Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!"); return Task.CompletedTask; } - - private Task LogAsync(LogMessage log) - { - Console.WriteLine(log.ToString()); - return Task.CompletedTask; - } } } diff --git a/samples/03_sharded_client/Services/CommandHandlingService.cs b/samples/03_sharded_client/Services/CommandHandlingService.cs index 1230cbcff..ec2e7f596 100644 --- a/samples/03_sharded_client/Services/CommandHandlingService.cs +++ b/samples/03_sharded_client/Services/CommandHandlingService.cs @@ -21,7 +21,6 @@ namespace _03_sharded_client.Services _services = services; _commands.CommandExecuted += CommandExecutedAsync; - _commands.Log += LogAsync; _discord.MessageReceived += MessageReceivedAsync; } @@ -61,12 +60,5 @@ namespace _03_sharded_client.Services // the command failed, let's notify the user that something happened. await context.Channel.SendMessageAsync($"error: {result.ToString()}"); } - - private Task LogAsync(LogMessage log) - { - Console.WriteLine(log.ToString()); - - return Task.CompletedTask; - } } } diff --git a/samples/idn/Program.cs b/samples/idn/Program.cs index ffd8fd1af..628fb9c08 100644 --- a/samples/idn/Program.cs +++ b/samples/idn/Program.cs @@ -34,60 +34,15 @@ namespace idn static async Task Main(string[] args) { var token = File.ReadAllText("token.ignore"); - var client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Debug }); - var logQueue = new ConcurrentQueue(); + var client = new DiscordSocketClient(new DiscordSocketConfig { }); var logCancelToken = new CancellationTokenSource(); int presenceUpdates = 0; - - client.Log += msg => - { - logQueue.Enqueue(msg); - return Task.CompletedTask; - }; + Console.CancelKeyPress += (_ev, _s) => { logCancelToken.Cancel(); }; - var logTask = Task.Run(async () => - { - var fs = new FileStream("idn.log", FileMode.Append); - var logStringBuilder = new StringBuilder(200); - string logString = ""; - - byte[] helloBytes = Encoding.UTF8.GetBytes($"### new log session: {DateTime.Now} ###\n\n"); - await fs.WriteAsync(helloBytes); - - while (!logCancelToken.IsCancellationRequested) - { - if (logQueue.TryDequeue(out var msg)) - { - if (msg.Message?.IndexOf("PRESENCE_UPDATE)") > 0) - { - presenceUpdates++; - continue; - } - - _ = msg.ToString(builder: logStringBuilder); - logStringBuilder.AppendLine(); - logString = logStringBuilder.ToString(); - - Debug.Write(logString, "DNET"); - await fs.WriteAsync(Encoding.UTF8.GetBytes(logString)); - } - await fs.FlushAsync(); - try - { - await Task.Delay(100, logCancelToken.Token); - } - finally { } - } - - byte[] goodbyeBytes = Encoding.UTF8.GetBytes($"#!! end log session: {DateTime.Now} !!#\n\n\n"); - await fs.WriteAsync(goodbyeBytes); - await fs.DisposeAsync(); - }); - await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); @@ -127,9 +82,9 @@ namespace idn await client.StopAsync(); client.Dispose(); logCancelToken.Cancel(); - try - { await logTask; } - finally { Console.WriteLine("goodbye!"); } + + await Task.Delay(-1, logCancelToken.Token); + Console.WriteLine("goodbye!"); } static IEnumerable GetAssemblies()