Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Console.WriteLine with proper logging #85

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Blazor.Gitter.Client/Blazor.Gitter.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Build" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="3.2.0" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
<PackageReference Include="Blazor.Extensions.Logging" Version="1.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions src/Blazor.Gitter.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Blazor.Extensions.Logging;
using Blazor.Gitter.Core;
using Blazor.Gitter.Core.Components;
using Blazor.Gitter.Core.Components.Shared;
using Blazor.Gitter.Library;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Blazor.Gitter.Client
{
Expand All @@ -16,11 +18,19 @@ public class Program
public static async Task Main(string[] args)
{
var builder = WebAssemblyHostBuilder.CreateDefault(args);

builder.Services.AddSingleton(new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) })
.AddSingleton<IChatApi, GitterApi>()
.AddSingleton<ILocalStorageService, LocalStorageService>()
.AddSingleton<ILocalisationHelper, LocalisationHelper>()
.AddSingleton<IAppState, AppState>();

builder.Services.AddLogging(builder => builder
.AddBrowserConsole()
.SetMinimumLevel(LogLevel.Trace)
.AddFilter("Microsoft.AspNetCore.*", level => level >= LogLevel.Information)
);

builder.RootComponents.Add<App>("app");

await builder.Build().RunAsync();
Expand Down
21 changes: 11 additions & 10 deletions src/Blazor.Gitter.Client/wwwroot/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Blazored Gitter - Client Side</title>
<base href="/" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="css/loader.css" rel="stylesheet" />
<link href="css/blazored.gitter.min.css" rel="stylesheet" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<script src="scripts/chat.js"></script>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Blazored Gitter - Client Side</title>
<base href="/" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css" integrity="sha384-oS3vJWv+0UjzBfQzYUhtDYW+Pj2yciDJxpsK1OYPAYjqT085Qq/1cq5FLXAZQ7Ay" crossorigin="anonymous">
<link href="css/loader.css" rel="stylesheet" />
<link href="css/blazored.gitter.min.css" rel="stylesheet" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<script src="scripts/chat.js"></script>
<script src="_content/Blazor.Extensions.Logging/blazor.extensions.logging.js" defer></script>
</head>
<body>
<app>
Expand Down
1 change: 1 addition & 0 deletions src/Blazor.Gitter.Core/Blazor.Gitter.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
Expand Down
16 changes: 9 additions & 7 deletions src/Blazor.Gitter.Core/Components/Shared/AppState.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Blazor.Gitter.Library;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
Expand All @@ -16,7 +17,7 @@ public class AppState : IAppState, IDisposable
private readonly ILocalStorageService LocalStorage;
private readonly ILocalisationHelper LocalisationHelper;
private readonly NavigationManager UriHelper;

private readonly ILogger<AppState> Log;
private string apiKey;
private IChatUser myUser;
private List<IChatRoom> myRooms;
Expand Down Expand Up @@ -76,15 +77,16 @@ public class AppState : IAppState, IDisposable
/// </summary>
public event EventHandler SearchMenuToggled;

public AppState(
ILocalStorageService localStorage,
ILocalisationHelper localisationHelper,
NavigationManager uriHelper
)
public AppState(ILocalStorageService localStorage,
ILocalisationHelper localisationHelper,
NavigationManager uriHelper,
ILogger<AppState> log)
{
LocalStorage = localStorage;
LocalisationHelper = localisationHelper;
UriHelper = uriHelper;
Log = log;

Task.Factory.StartNew(Initialise);
}

Expand All @@ -108,7 +110,7 @@ public async Task Initialise()
{
if (done == 1)
{
Console.WriteLine(ex);
Log.LogError(ex, "Couldn't initialize AppState");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Blazor.Gitter.Core.Browser;
using Blazor.Gitter.Library;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using System;
using System.Collections.Generic;
Expand All @@ -16,6 +17,7 @@ public class RoomMessagesBase : ComponentBase, IDisposable
[Inject] IChatApi GitterApi { get; set; }
[Inject] ILocalisationHelper Localisation { get; set; }
[Inject] IAppState State { get; set; }
[Inject] ILogger<RoomMessagesBase> Log { get; set; }

[Parameter] public IChatRoom ChatRoom { get; set; }
[Parameter] public string UserId { get; set; }
Expand Down Expand Up @@ -224,9 +226,9 @@ async Task<int> FetchNewMessages(IChatMessageOptions options, CancellationToken
await Task.Delay(1);
}
}
catch (Exception e)
catch (Exception ex)
{
Console.WriteLine($"RoomMessages.FetchNewMessages: {e.GetBaseException().Message}");
Log.LogError(ex, "Failed to fetch new room messages");
}
finally
{
Expand Down
13 changes: 8 additions & 5 deletions src/Blazor.Gitter.Core/Services/LocalisationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.JSInterop;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using System;
using System.Globalization;
using System.Threading.Tasks;
Expand All @@ -10,13 +11,15 @@ public class LocalisationHelper : ILocalisationHelper
private TimeZoneInfo localTimeZoneInfo;
private CultureInfo localCultureInfo;
private IJSRuntime JSRuntime;
private readonly ILogger<ILocalisationHelper> Log;

public TimeZoneInfo LocalTimeZoneInfo => localTimeZoneInfo ?? TimeZoneInfo.Local;
public CultureInfo LocalCultureInfo => localCultureInfo ?? CultureInfo.CurrentCulture;

public LocalisationHelper(IJSRuntime jSRuntime)
public LocalisationHelper(IJSRuntime jSRuntime, ILogger<ILocalisationHelper> log)
{
JSRuntime = jSRuntime;
Log = log;
}
public async Task BuildLocalTimeZone()
{
Expand All @@ -28,7 +31,7 @@ public async Task BuildLocalTimeZone()
}
catch (Exception ex)
{
Console.WriteLine(ex);
Log.LogError(ex, "Failed to build local time zone");
localTimeZoneInfo = TimeZoneInfo.Local;
}
}
Expand All @@ -41,7 +44,7 @@ public async Task BuildLocalCulture()
}
catch (Exception ex)
{
Console.WriteLine(ex);
Log.LogError(ex, "Failed to build local culture info");
localCultureInfo = CultureInfo.CurrentCulture;
}
}
Expand All @@ -54,7 +57,7 @@ public async Task<string> GetKey(string key)
}
catch (Exception ex)
{
Console.WriteLine(ex);
Log.LogError(ex, "Failed to get key");
return string.Empty;
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Blazor.Gitter.Core/browser/BrowserInterop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using Microsoft.JSInterop;
using System;
using System.Threading.Tasks;
Expand All @@ -11,15 +12,17 @@ public static ValueTask<double> GetScrollTop(this IJSRuntime JSRuntime, string i
{
return JSRuntime.InvokeAsync<double>("chat.getScrollTop",id);
}
public static ValueTask<bool> IsScrolledToBottom(this IJSRuntime JSRuntime, string id)
public static ValueTask<bool> IsScrolledToBottom(this IJSRuntime JSRuntime,
ILogger log,
string id)
{
try
{
return JSRuntime.InvokeAsync<bool>("chat.isScrolledToBottom", id);
}
catch (Exception ex)
{
Console.WriteLine($"BrowserInterop.IsScrolledToBottom: {ex.GetBaseException().Message}");
log.LogError(ex, "BrowserInterop.IsScrolledToBottom failed");
}
return new ValueTask<bool>(Task.FromResult(false));
}
Expand Down
1 change: 1 addition & 0 deletions src/Blazor.Gitter.Library/Blazor.Gitter.Library.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<PackageReference Include="System.Net.Http.Json" Version="3.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Http" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
</ItemGroup>

</Project>
14 changes: 10 additions & 4 deletions src/Blazor.Gitter.Library/Services/GitterApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;

namespace Blazor.Gitter.Library
{
Expand All @@ -16,9 +17,13 @@ public class GitterApi : IChatApi

private string Token { get; set; }
private HttpClient HttpClient { get; set; }
public GitterApi(HttpClient httpClient = null)

private readonly ILogger<GitterApi> Log;

public GitterApi(HttpClient httpClient = null, ILogger<GitterApi> log = null)
{
HttpClient = httpClient ?? throw new Exception("Make sure you have added an HttpClient to your DI Container");
Log = log;
}

public void SetAccessToken(string token)
Expand All @@ -41,11 +46,12 @@ public async Task<IChatUser> GetCurrentUser()
{
try
{
Console.WriteLine("Fetching gitter user.");
Log.LogDebug("Fetching gitter user.");
return (await HttpClient.GetFromJsonAsync<GitterUser[]>(APIUSERPATH)).First();
} catch (Exception ex)
}
catch (Exception ex)
{
Console.WriteLine(ex);
Log.LogError(ex, "Failed to fetch gitter user");
throw;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Blazor.Gitter.Server/Blazor.Gitter.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.4" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.4" />
</ItemGroup>

<ItemGroup>
Expand Down