Browse Source

Fix example logging

pull/1765/head
Waterball 4 years ago
parent
commit
fe4c6fbd45
5 changed files with 6 additions and 83 deletions
  1. +1
    -8
      samples/01_basic_ping_bot/Program.cs
  2. +0
    -10
      samples/02_commands_framework/Program.cs
  3. +0
    -7
      samples/03_sharded_client/Program.cs
  4. +0
    -8
      samples/03_sharded_client/Services/CommandHandlingService.cs
  5. +5
    -50
      samples/idn/Program.cs

+ 1
- 8
samples/01_basic_ping_bot/Program.cs View File

@@ -31,8 +31,7 @@ namespace _01_basic_ping_bot
// It is recommended to Dispose of a client when you are finished // It is recommended to Dispose of a client when you are finished
// using it, at the end of your app's lifetime. // using it, at the end of your app's lifetime.
_client = new DiscordSocketClient(); _client = new DiscordSocketClient();

_client.Log += LogAsync;
_client.Ready += ReadyAsync; _client.Ready += ReadyAsync;
_client.MessageReceived += MessageReceivedAsync; _client.MessageReceived += MessageReceivedAsync;
} }
@@ -47,12 +46,6 @@ namespace _01_basic_ping_bot
await Task.Delay(Timeout.Infinite); 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 // The Ready event indicates that the client has opened a
// connection and it is now safe to access the cache. // connection and it is now safe to access the cache.
private Task ReadyAsync() private Task ReadyAsync()


+ 0
- 10
samples/02_commands_framework/Program.cs View File

@@ -35,9 +35,6 @@ namespace _02_commands_framework
{ {
var client = services.GetRequiredService<DiscordSocketClient>(); var client = services.GetRequiredService<DiscordSocketClient>();


client.Log += LogAsync;
services.GetRequiredService<CommandService>().Log += LogAsync;

// Tokens should be considered secret data and never hard-coded. // Tokens should be considered secret data and never hard-coded.
// We can read from the environment variable to avoid hardcoding. // We can read from the environment variable to avoid hardcoding.
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("token")); 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() private ServiceProvider ConfigureServices()
{ {
return new ServiceCollection() return new ServiceCollection()


+ 0
- 7
samples/03_sharded_client/Program.cs View File

@@ -38,7 +38,6 @@ namespace _03_sharded_client
// The ShardReady event is used instead, allowing for individual // The ShardReady event is used instead, allowing for individual
// control per shard. // control per shard.
client.ShardReady += ReadyAsync; client.ShardReady += ReadyAsync;
client.Log += LogAsync;


await services.GetRequiredService<CommandHandlingService>().InitializeAsync(); await services.GetRequiredService<CommandHandlingService>().InitializeAsync();


@@ -65,11 +64,5 @@ namespace _03_sharded_client
Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!"); Console.WriteLine($"Shard Number {shard.ShardId} is connected and ready!");
return Task.CompletedTask; return Task.CompletedTask;
} }

private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());
return Task.CompletedTask;
}
} }
} }

+ 0
- 8
samples/03_sharded_client/Services/CommandHandlingService.cs View File

@@ -21,7 +21,6 @@ namespace _03_sharded_client.Services
_services = services; _services = services;


_commands.CommandExecuted += CommandExecutedAsync; _commands.CommandExecuted += CommandExecutedAsync;
_commands.Log += LogAsync;
_discord.MessageReceived += MessageReceivedAsync; _discord.MessageReceived += MessageReceivedAsync;
} }


@@ -61,12 +60,5 @@ namespace _03_sharded_client.Services
// the command failed, let's notify the user that something happened. // the command failed, let's notify the user that something happened.
await context.Channel.SendMessageAsync($"error: {result.ToString()}"); await context.Channel.SendMessageAsync($"error: {result.ToString()}");
} }

private Task LogAsync(LogMessage log)
{
Console.WriteLine(log.ToString());

return Task.CompletedTask;
}
} }
} }

+ 5
- 50
samples/idn/Program.cs View File

@@ -34,60 +34,15 @@ namespace idn
static async Task Main(string[] args) static async Task Main(string[] args)
{ {
var token = File.ReadAllText("token.ignore"); var token = File.ReadAllText("token.ignore");
var client = new DiscordSocketClient(new DiscordSocketConfig { LogLevel = LogSeverity.Debug });
var logQueue = new ConcurrentQueue<LogMessage>();
var client = new DiscordSocketClient(new DiscordSocketConfig { });
var logCancelToken = new CancellationTokenSource(); var logCancelToken = new CancellationTokenSource();
int presenceUpdates = 0; int presenceUpdates = 0;

client.Log += msg =>
{
logQueue.Enqueue(msg);
return Task.CompletedTask;
};
Console.CancelKeyPress += (_ev, _s) => Console.CancelKeyPress += (_ev, _s) =>
{ {
logCancelToken.Cancel(); 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.LoginAsync(TokenType.Bot, token);
await client.StartAsync(); await client.StartAsync();


@@ -127,9 +82,9 @@ namespace idn
await client.StopAsync(); await client.StopAsync();
client.Dispose(); client.Dispose();
logCancelToken.Cancel(); logCancelToken.Cancel();
try
{ await logTask; }
finally { Console.WriteLine("goodbye!"); }
await Task.Delay(-1, logCancelToken.Token);
Console.WriteLine("goodbye!");
} }


static IEnumerable<Assembly> GetAssemblies() static IEnumerable<Assembly> GetAssemblies()


Loading…
Cancel
Save