Skip to content

Commit

Permalink
add logging
Browse files Browse the repository at this point in the history
  • Loading branch information
georg-jung committed May 19, 2020
1 parent a92b782 commit 3d7efd1
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 5 deletions.
130 changes: 130 additions & 0 deletions WoL/Extensions/LoggingExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using WoL.Data;
using WoL.Models.ViewModels;

namespace WoL.Extensions
{
// to be accessible from razor views this needs to be public
public static class LoggingExtensions
{
// this class is build like this one from the framework:
// https://github.com/dotnet/aspnetcore/blob/f2e6e6ff334176540ef0b3291122e359c2106d1a/src/Security/Authentication/Core/src/LoggingExtensions.cs

private static readonly Action<ILogger, Exception> addHostUnknownException;
private static readonly Action<ILogger, Models.Host, Exception> addHostDuplicateEntryException;
private static readonly Action<ILogger, Exception> getHostPlatformNotSupportedException;
private static readonly Action<ILogger, string, string, string, Exception> getHostUnknownException;
private static readonly Action<ILogger, string, string, string, Exception> getHostHostNotFound;
private static readonly Action<ILogger, Models.Host, Exception> hostAdded;
private static readonly Action<ILogger, Models.Host, Exception> wakeHost;
private static readonly Action<ILogger, Models.Host, int, HostViewModel.HostStatus, Exception> wakeHostFinalStatus;
private static readonly Action<ILogger, Models.Host, Exception> hostDeleted;

static LoggingExtensions()
{
addHostUnknownException = LoggerMessage.Define(
LogLevel.Error,
new EventId(1, "AddHostUnknownException"),
"An unknown exception was thrown when adding a host to the HostService."
);

addHostDuplicateEntryException = LoggerMessage.Define<Models.Host>(
LogLevel.Information,
new EventId(2, "AddHostDuplicateEntryException"),
"The host {Host} could not be added as one of it's entries that should be unique weren't."
);

getHostPlatformNotSupportedException = LoggerMessage.Define(
LogLevel.Warning,
new EventId(3, "GetHostPlatformNotSupportedException"),
"A PlatformNotSupported exception was thrown when trying to get a host for the user's input."
);

getHostUnknownException = LoggerMessage.Define<string, string, string>(
LogLevel.Warning,
new EventId(4, "GetHostUnknownException"),
"An unknown exception was thrown while resolving the host '{CaptionInput}' {HostNameInput} / {MacAddressInput}."
);

getHostHostNotFound = LoggerMessage.Define<string, string, string>(
LogLevel.Information,
new EventId(5, "GetHostUnknownException"),
"While resolving '{CaptionInput}' {HostNameInput} / {MacAddressInput} no host could be found."
);

hostAdded = LoggerMessage.Define<Models.Host>(
LogLevel.Information,
new EventId(10, "HostAdded"),
"Host {Host} was added successfully."
);

wakeHost = LoggerMessage.Define<Models.Host>(
LogLevel.Information,
new EventId(20, "WakeHost"),
"Waking {Host}..."
);

wakeHostFinalStatus = LoggerMessage.Define<Models.Host, int, HostViewModel.HostStatus>(
LogLevel.Information,
new EventId(21, "WakeHostFinalStatus"),
"Final HostStatus of {Host} after {PingTries} ping tries is {HostStatus}."
);

hostDeleted = LoggerMessage.Define<Models.Host>(
LogLevel.Information,
new EventId(30, "HostDeleted"),
"Host {Host} deleted."
);
}

public static void AddHostUnknownException(this ILogger logger, Exception exc)
{
addHostUnknownException(logger, exc);
}

public static void AddHostDuplicateEntryException(this ILogger logger, Models.Host value, IHostService.DuplicateEntryException exc)
{
addHostDuplicateEntryException(logger, value, exc);
}

public static void GetHostPlatformNotSupportedException(this ILogger logger, PlatformNotSupportedException exc)
{
getHostPlatformNotSupportedException(logger, exc);
}

public static void GetHostUnknownException(this ILogger logger, string captionInput, string hostNameInput, string macAddressInput, Exception exc)
{
getHostUnknownException(logger, captionInput, hostNameInput, macAddressInput, exc);
}

public static void GetHostHostNotFound(this ILogger logger, string captionInput, string hostNameInput, string macAddressInput, Exception exc)
{
getHostHostNotFound(logger, captionInput, hostNameInput, macAddressInput, exc);
}

public static void HostAdded(this ILogger logger, Models.Host host)
{
hostAdded(logger, host, null);
}

public static void WakeHost(this ILogger logger, Models.Host host)
{
wakeHost(logger, host, null);
}

public static void WakeHostFinalStatus(this ILogger logger, Models.Host host, int pingTries, HostViewModel.HostStatus finalStatus)
{
wakeHostFinalStatus(logger, host, pingTries, finalStatus, null);
}

public static void HostDeleted(this ILogger logger, Models.Host host)
{
hostDeleted(logger, host, null);
}
}
}
6 changes: 6 additions & 0 deletions WoL/Models/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using WoL.Extensions;

namespace WoL.Models
{
Expand All @@ -21,5 +22,10 @@ public class Host
[Required]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Eigenschaften dürfen keine Arrays zurückgeben", Justification = "A mac address intrinsically is a byte[]")]
public byte[] MacAddress { get; set; }

public override string ToString()
{
return $"Host(Id = {Id}; Hostname = {Hostname}; MacAddress = {(MacAddress == null ? "null" : this.GetMacString())}; Caption = {Caption})";
}
}
}
22 changes: 19 additions & 3 deletions WoL/Pages/AddHost.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@using WoL.Services
@using WoL.Extensions
@using Microsoft.Extensions.Logging
@using System.Net.Sockets
@inject IHostService HostService
@inject IAddressLookupService AddressService
@inject NavigationManager NavigationManager
Expand Down Expand Up @@ -117,30 +118,45 @@
}
catch (PlatformNotSupportedException ex)
{
L.LogInformation(ex, $"PlatformNotSupported during name resolution or determination of mac address. Page: {nameof(AddHost)}");
L.GetHostPlatformNotSupportedException(ex);
Alert = "This operation is not supported on the operating system this application is running on. Adding hosts by hostname is currently not supported on Mac OS X due to the lack of an appropriate ARP API.";
Creating = false;
return;
}
catch (SocketException sockEx) when (sockEx.ErrorCode == 11001)
{
// https://docs.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2
/* WSAHOST_NOT_FOUND
Host not found.
No such host is known. The name is not an official host name or alias, or it cannot be found in the database(s) being queried. This error may also be returned for protocol and service queries, and means that the specified name could not be found in the relevant database.
*/
L.GetHostHostNotFound(model.Caption, model.Hostname, model.MacAddress, sockEx);
Alert = "This hostname could not be found.";
Creating = false;
return;
}
catch (Exception ex)
{
L.LogDebug(ex, $"Exception during name resolution or determination of mac address. Page: {nameof(AddHost)}");
L.GetHostUnknownException(model.Caption, model.Hostname, model.MacAddress, ex);
Alert = "This hostname or it's mac address could not be found.";
Creating = false;
return;
}
try
{
await HostService.Add(host);
L.HostAdded(host);
}
catch (IHostService.DuplicateEntryException duplEx)
{
L.AddHostDuplicateEntryException(host, duplEx);
Alert = $"Creation failed as an entry with {duplEx.Field.ToLower()} '{duplEx.Value}' does already exists.";
Creating = false;
return;
}
catch
catch (Exception ex)
{
L.AddHostUnknownException(ex);
Alert = "This entry could not be created. Maybe a similar entry which shares some of the entered values does already exist. Please contact the administrator.";
Creating = false;
return;
Expand Down
3 changes: 3 additions & 0 deletions WoL/Pages/DeleteHost.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
@using WoL.Models
@using WoL.Services
@using WoL.Extensions
@using Microsoft.Extensions.Logging
@inject IHostService HostService
@inject IAddressLookupService AddressService
@inject NavigationManager NavigationManager
@inject ILogger<DeleteHost> L

<div class="container">
<h2>Delete Host</h2>
Expand Down Expand Up @@ -78,6 +80,7 @@
{
Deleting = true;
await HostService.Delete(Id);
L.HostDeleted(model);
NavigationManager.NavigateTo("/");
}
}
7 changes: 5 additions & 2 deletions WoL/Pages/Wake.razor
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
@page "/Wake/{Id:int}"
@page "/Wake/{Id:int}"

@using WoL.Data
@using WoL.Models
@using WoL.Models.ViewModels
@using WoL.Services
@using WoL.Extensions
@using WoL.Components
@using Microsoft.Extensions.Logging
@inject IHostService HostService
@inject IWakeService WakeService
@inject IPingService PingService
@inject ILogger<Wake> L

<div class="container">
<h2>Wake</h2>
Expand Down Expand Up @@ -150,6 +152,7 @@
Danger = "This host does not exist.";
return;
}
L.WakeHost(host);
model = new HostViewModel(host);
AlertSpinner = true;
Info = "Sending wake-up packet...";
Expand All @@ -171,7 +174,7 @@
await Task.Delay(minInterval - msecSince);
await PingAndSetStatus();
}

L.WakeHostFinalStatus(host, PingTries, model.Status);
IsCheckingStatus = false;
base.StateHasChanged();
}
Expand Down

0 comments on commit 3d7efd1

Please sign in to comment.