Use ILogger instead of Console.WriteLine

This commit is contained in:
rafael-aero
2021-08-26 09:55:14 +02:00
parent 2f0229235b
commit 44d979fef4
7 changed files with 50 additions and 19 deletions

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using SocketIOClient;
@@ -97,7 +98,7 @@ namespace ElectronNET.API
{
if (App.SocketDebug)
{
Console.WriteLine($"Sending event {eventString}");
Log("Sending event {0}", eventString);
}
await _socketSemaphore.WaitAsync();
@@ -112,11 +113,23 @@ namespace ElectronNET.API
if (App.SocketDebug)
{
Console.WriteLine($"Sent event {eventString}");
Log($"Sent event {eventString}");
}
});
}
internal static void Log(string formatString, params object[] args)
{
if (Logger is object)
{
Logger.LogInformation(formatString, args);
}
else
{
Console.WriteLine(formatString, args);
}
}
/// <summary>
/// This method is only used on places where we need to be sure the event was sent on the socket, such as Quit, Exit, Relaunch and QuitAndInstall methods
/// </summary>
@@ -126,7 +139,7 @@ namespace ElectronNET.API
{
if (App.SocketDebug)
{
Console.WriteLine($"Sending event {eventString}");
Log("Sending event {0}", eventString);
}
_socketSemaphore.Wait();
@@ -143,7 +156,7 @@ namespace ElectronNET.API
if (App.SocketDebug)
{
Console.WriteLine($"Sent event {eventString}");
Log("Sent event {0}", eventString);
}
}
@@ -328,28 +341,28 @@ namespace ElectronNET.API
socket.OnConnected += (_, __) =>
{
Console.WriteLine("ElectronNET socket connected!");
Log("ElectronNET socket connected on port {0}!", BridgeSettings.SocketPort);
};
socket.OnReconnectAttempt += (_, __) =>
{
Console.WriteLine("ElectronNET socket is trying to reconnect...");
Log("ElectronNET socket is trying to reconnect on port {0}...", BridgeSettings.SocketPort);
};
socket.OnReconnectError += (_, ex) =>
{
Console.WriteLine("ElectronNET socket failed to connect {0}", ex.ToString());
Log("ElectronNET socket failed to connect {0}", ex);
};
socket.OnReconnected += (_, __) =>
{
Console.WriteLine("ElectronNET socket reconnected...");
Log("ElectronNET socket reconnected on port {0}...", BridgeSettings.SocketPort);
};
socket.OnDisconnected += (_, __) =>
{
Console.WriteLine("ElectronNET socket disconnected, trying to reconnect!");
Log("ElectronNET socket disconnected, trying to reconnect on port {0}!", on port { 0});
socket.ConnectAsync().Wait();
};
@@ -369,6 +382,8 @@ namespace ElectronNET.API
}
}
internal static ILogger<App> Logger { private get; set; }
private class CamelCaseNewtonsoftJsonSerializer : NewtonsoftJsonSerializer
{
public CamelCaseNewtonsoftJsonSerializer(int eio) : base(eio)

View File

@@ -1,10 +1,25 @@
namespace ElectronNET.API
using Microsoft.Extensions.Logging;
namespace ElectronNET.API
{
/// <summary>
/// The Electron.NET API
/// </summary>
public static class Electron
{
private static ILoggerFactory loggerFactory;
/// <summary>
/// Sets the logger factory to be used by Electron, if any
/// </summary>
public static ILoggerFactory LoggerFactory
{
private get => loggerFactory; set
{
loggerFactory = value;
BridgeConnector.Logger = value.CreateLogger<App>();
}
}
/// <summary>
/// Communicate asynchronously from the main process to renderer processes.
/// </summary>

View File

@@ -36,6 +36,7 @@ This package contains the API to access the "native" electron API.</Description>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>

View File

@@ -434,7 +434,7 @@ namespace ElectronNET.API.Entities
}
catch (Exception ex)
{
Console.WriteLine(ex);
BridgeConnector.LogError(ex, "Error getting scaled images");
}
return dict;

View File

@@ -16,7 +16,7 @@ namespace ElectronNET.API
{
App.Instance.IsReady = true;
Console.WriteLine("ASP.NET Core host has fully started.");
BridgeConnector.Log("ASP.NET Core host has fully started.");
});
}

View File

@@ -23,7 +23,7 @@ namespace ElectronNET.API
if (argument.ToUpper().Contains("ELECTRONPORT"))
{
BridgeSettings.SocketPort = argument.ToUpper().Replace("/ELECTRONPORT=", "");
Console.WriteLine("Use Electron Port: " + BridgeSettings.SocketPort);
BridgeConnector.Log("Use Electron Port: " + BridgeSettings.SocketPort);
}
else if (argument.ToUpper().Contains("ELECTRONWEBPORT"))
{