Skip to content

Commit

Permalink
Use Serilog for logging to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Bond-009 committed Apr 22, 2017
1 parent 7ba0293 commit 9b3447e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Just a Discord bot
| invite | | | Returns the OAuth2 Invite URL of the bot |
| leave | | GuildPermission.ManageGuild | Instructs the bot to leave this Guild |
| say | echo | | Echos the provided input |
| setgame | | **Trusted user** | Sets the bot's game |
| setgame | | **Trusted user** | Sets the bots game |
| ping | | | Gets the estimated round-trip latency, in milliseconds, to the gateway server |
| userinfo | | | Returns info about the user |

## Administration commands
Expand All @@ -30,7 +31,7 @@ Just a Discord bot
| Command | Aliases | Permission | Description |
| ------- | ------- | ---------- | ----------- |
| join | | | Joins the voice channel |
| stop | | | Stops the audio blayback and leaves the voice channel |
| stop | | | Stops the audio playback and leaves the voice channel |
| play | | | Plays an audio files |

## Core commands
Expand Down Expand Up @@ -74,8 +75,10 @@ Just a Discord bot

| Command | Aliases | Permission | Description |
| ------- | ------- | ---------- | ----------- |
| createtag | | GuildPermission.ManageMessages | Creates a new tag |
| tag create| createtag | GuildPermission.ManageMessages | Creates a new tag |
| tag | | | Searches for a tag |
| tag delete | tag remove, deletetag, removetag | | Deletes a tag |
| tag list | tags list, listtags | | Lists all tags |

## Weather commands

Expand Down
14 changes: 8 additions & 6 deletions src/iTool.DiscordBot/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Discord;
using Microsoft.Extensions.Logging;
using Serilog;
using System;
using System.IO;
using System.Threading.Tasks;
Expand All @@ -8,9 +8,9 @@ namespace iTool.DiscordBot
{
public static class Logger
{
static ILogger logger = new LoggerFactory()
.AddFile(Common.LogsDir + Path.DirectorySeparatorChar + "log-{Date}.log")
.CreateLogger(string.Empty);
static ILogger logger = new LoggerConfiguration()
.WriteTo.RollingFile(Common.LogsDir + Path.DirectorySeparatorChar + "log-{Date}.log")
.CreateLogger();
public static LogSeverity LogLevel { get; internal set; } = LogSeverity.Verbose;

public static Task Log(LogMessage msg)
Expand All @@ -22,17 +22,19 @@ public static Task Log(LogMessage msg)
{
case LogSeverity.Critical:
case LogSeverity.Error:
logger.LogError($"{msg.Source}: {msg.Message}");
logger.Error($"{msg.Source}: {msg.Message}");
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogSeverity.Warning:
logger.Warning($"{msg.Source}: {msg.Message}");
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogSeverity.Info:
logger.LogInformation($"{msg.Source}: {msg.Message}");
logger.Information($"{msg.Source}: {msg.Message}");
break;
case LogSeverity.Verbose:
case LogSeverity.Debug:
logger.Debug($"{msg.Source}: {msg.Message}");
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/iTool.DiscordBot/Modules/AudioModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task Join()
=> await AudioService.JoinAudio(Context.Guild, (Context.User as IGuildUser).VoiceChannel);

[Command("stop", RunMode = RunMode.Async)]
[Summary("Stops the audio blayback and leaves the voice channel")]
[Summary("Stops the audio playback and leaves the voice channel")]
[RequireContext(ContextType.Guild)]
public async Task Stop()
=> await AudioService.LeaveAudio(Context.Guild);
Expand Down
2 changes: 1 addition & 1 deletion src/iTool.DiscordBot/Modules/TagModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public async Task TagDelete(string name)
{
LiteCollection<Tag> col = db.GetCollection<Tag>("tags");
int? id = col.Find(x => x.Title == name
&& x.Author == Context.User.Id)
&& (x.Author == Context.User.Id || Context.User.Id == Context.Guild.OwnerId))
.FirstOrDefault()?.Id;

if (id == null)
Expand Down
4 changes: 1 addition & 3 deletions src/iTool.DiscordBot/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using Microsoft.Extensions.Logging;
using System;
using System;
using System.Threading;

namespace iTool.DiscordBot
{
public static class Program
{
static bool running = true;
public static ILoggerFactory LoggerFactory;

public static void Main(string[] args)
{
Expand Down
3 changes: 1 addition & 2 deletions src/iTool.DiscordBot/iTool.DiscordBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
<PackageReference Include="Discord.Net.WebSocket" Version="1.0.0-rc2-00702" />
<PackageReference Include="HOTSLogs.Net" Version="0.1.0" />
<PackageReference Include="LiteDB" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="OpenWeather.Net" Version="0.6.4" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="YamlDotNet.NetCore" Version="1.0.0" />
</ItemGroup>

Expand Down

0 comments on commit 9b3447e

Please sign in to comment.