Logging execution times of commands.

This commit is contained in:
Kwoth 2016-07-26 01:00:17 +02:00
parent bbf2789704
commit 76fc3be986

View File

@ -4,8 +4,10 @@ using NadekoBot.Extensions;
using NadekoBot.Modules.Administration.Commands; using NadekoBot.Modules.Administration.Commands;
using NadekoBot.Modules.Music; using NadekoBot.Modules.Music;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Reflection; using System.Reflection;
@ -43,6 +45,12 @@ namespace NadekoBot
statsStopwatch.Start(); statsStopwatch.Start();
commandService.CommandExecuted += StatsCollector_RanCommand; commandService.CommandExecuted += StatsCollector_RanCommand;
commandService.CommandFinished += CommandService_CommandFinished;
File.Create("errors.txt");
commandService.CommandErrored += (s, e) => File.AppendAllText("errors.txt", $@"Command: {e.Command}
{e.Exception}
-------------------------------------
");
Task.Run(StartCollecting); Task.Run(StartCollecting);
@ -107,6 +115,7 @@ namespace NadekoBot
carbonStatusTimer.Elapsed += async (s, e) => await SendUpdateToCarbon().ConfigureAwait(false); carbonStatusTimer.Elapsed += async (s, e) => await SendUpdateToCarbon().ConfigureAwait(false);
carbonStatusTimer.Start(); carbonStatusTimer.Start();
} }
HttpClient carbonClient = new HttpClient(); HttpClient carbonClient = new HttpClient();
private async Task SendUpdateToCarbon() private async Task SendUpdateToCarbon()
{ {
@ -177,9 +186,11 @@ namespace NadekoBot
private async Task StartCollecting() private async Task StartCollecting()
{ {
var statsSw = new Stopwatch();
while (true) while (true)
{ {
await Task.Delay(new TimeSpan(0, 30, 0)).ConfigureAwait(false); await Task.Delay(new TimeSpan(0, 30, 0)).ConfigureAwait(false);
statsSw.Start();
try try
{ {
var onlineUsers = await Task.Run(() => NadekoBot.Client.Servers.Sum(x => x.Users.Count())).ConfigureAwait(false); var onlineUsers = await Task.Run(() => NadekoBot.Client.Servers.Sum(x => x.Users.Count())).ConfigureAwait(false);
@ -196,6 +207,13 @@ namespace NadekoBot
ConnectedServers = connectedServers, ConnectedServers = connectedServers,
DateAdded = DateTime.Now DateAdded = DateTime.Now
}); });
statsSw.Stop();
var clr = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine($"--------------\nCollecting stats finished in {statsSw.Elapsed.TotalSeconds}s\n-------------");
Console.ForegroundColor = clr;
statsSw.Reset();
} }
catch catch
{ {
@ -205,9 +223,20 @@ namespace NadekoBot
} }
} }
private static ConcurrentDictionary<ulong, DateTime> commandTracker = new ConcurrentDictionary<ulong, DateTime>();
private void CommandService_CommandFinished(object sender, CommandEventArgs e)
{
DateTime dt;
if (!commandTracker.TryGetValue(e.Message.Id, out dt))
return;
Console.WriteLine($">>COMMAND ENDED after *{(DateTime.UtcNow - dt).TotalSeconds}s*\nCmd: {e.Command.Text}\nMsg: {e.Message.Text}\nUsr: {e.User.Name} [{e.User.Id}]\nSrvr: {e.Server?.Name ?? "PRIVATE"} [{e.Server?.Id}]\n-----");
}
private async void StatsCollector_RanCommand(object sender, CommandEventArgs e) private async void StatsCollector_RanCommand(object sender, CommandEventArgs e)
{ {
Console.WriteLine($">> Cmd: {e.Command.Text}\nMsg: {e.Message.Text}\nUsr: {e.User.Name} [{e.User.Id}]\nSrvr: {e.Server?.Name ?? "PRIVATE"} [{e.Server?.Id}]\n-----"); commandTracker.TryAdd(e.Message.Id, DateTime.UtcNow);
Console.WriteLine($">>COMMAND STARTED\nCmd: {e.Command.Text}\nMsg: {e.Message.Text}\nUsr: {e.User.Name} [{e.User.Id}]\nSrvr: {e.Server?.Name ?? "PRIVATE"} [{e.Server?.Id}]\n-----");
#if !NADEKO_RELEASE #if !NADEKO_RELEASE
await Task.Run(() => await Task.Run(() =>
{ {