Skip to content

Commit

Permalink
[Add] Serilog logging and replace console.writeline with logger state…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
sam.gerene committed Dec 29, 2021
1 parent 01b8ba4 commit 02fa5ef
Show file tree
Hide file tree
Showing 24 changed files with 111 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DevExpress.Xpo" Version="20.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="5.0.13" />
<PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
<PackageReference Include="ReqIFSharp" Version="5.0.0-rc4" />
</ItemGroup>

Expand Down
46 changes: 42 additions & 4 deletions ReqifViewer.Infrastructure/Services/ReqIFLoaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ namespace ReqifViewer.Infrastructure.Services
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

using ReqIFSharp;

/// <summary>
Expand All @@ -35,8 +39,27 @@ namespace ReqifViewer.Infrastructure.Services
/// </summary>
public class ReqIFLoaderService : IReqIFLoaderService
{
/// <summary>
/// the <see cref="Stream"/> that holds a copy of the stream the reqif was loaded from
/// </summary>
private Stream sourceStream;

/// <summary>
/// The <see cref="ILogger"/> used to log
/// </summary>
private readonly ILogger<ReqIFLoaderService> logger;

/// <summary>
/// Initializes a new instance of the <see cref="ReqIFLoaderService"/>
/// </summary>
/// <param name="logger">
/// The (injected) logger
/// </param>
public ReqIFLoaderService(ILogger<ReqIFLoaderService> logger = null)
{
this.logger = logger ?? NullLogger<ReqIFLoaderService>.Instance;
}

/// <summary>
/// a thread safe cache where the data associated to an <see cref="ExternalObject"/> is stored
/// </summary>
Expand Down Expand Up @@ -88,13 +111,17 @@ public async Task Load(Stream reqIFStream, CancellationToken token)

var deserializationStream = new MemoryStream();

this.logger.LogDebug("copying the reqif stream to the deserialization stream for deserialization");

reqIFStream.Seek(0, SeekOrigin.Begin);
await reqIFStream.CopyToAsync(deserializationStream, token);
if (deserializationStream.Position != 0)
{
deserializationStream.Seek(0, SeekOrigin.Begin);
}

this.logger.LogDebug("copying the reqif stream to the source stream for safe keeping");

reqIFStream.Seek(0, SeekOrigin.Begin);
await reqIFStream.CopyToAsync(this.sourceStream, token);
if (this.sourceStream.Position != 0)
Expand All @@ -105,8 +132,12 @@ public async Task Load(Stream reqIFStream, CancellationToken token)
IEnumerable<ReqIF> result = null;

var reqIfDeserializer = new ReqIFDeserializer();

var sw = Stopwatch.StartNew();
this.logger.LogDebug("starting deserialization");
result = await reqIfDeserializer.DeserializeAsync(deserializationStream, token);

this.logger.LogDebug("deserialization finished in {time} [ms]", sw.ElapsedMilliseconds);

await deserializationStream.DisposeAsync();

this.ReqIFData = result;
Expand Down Expand Up @@ -138,6 +169,8 @@ public async Task<string> QueryData(ExternalObject externalObject, CancellationT

if (externalObjectDataCache.TryGetValue(externalObject, out var result))
{
this.logger.LogDebug("External Object {uri} retrieved from Cache", externalObject.Uri);

return result;
}

Expand All @@ -149,21 +182,26 @@ public async Task<string> QueryData(ExternalObject externalObject, CancellationT

result = $"data:{externalObject.MimeType};base64,{Convert.ToBase64String(targetStream.ToArray())}";

this.externalObjectDataCache.TryAdd(externalObject, result);

if (this.externalObjectDataCache.TryAdd(externalObject, result))
{
this.logger.LogDebug("External Object {uri} added to Cache", externalObject.Uri);
}

return result;
}

/// <summary>
/// Resets the <see cref="ReqIFLoaderService"/> by clearing <see cref="ReqIFData"/> and
/// <see cref="SourceStream"/>
/// <see cref="sourceStream"/>
/// </summary>
public void Reset()
{
this.sourceStream = null;
this.ReqIFData = null;
this.externalObjectDataCache.Clear();

this.logger.LogDebug("loader service reset");

ReqIfChanged?.Invoke(this, null);
}

Expand Down
5 changes: 3 additions & 2 deletions reqifviewer/Components/AttributeDefinitionsComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@using ReqIFSharp;
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using Serilog;

<RadzenDataGrid Data="@this.AttributeDefinitions" AllowFiltering="true" AllowColumnResize="true" TItem="AttributeDefinition">
<Columns>
Expand Down Expand Up @@ -54,7 +55,7 @@
/// The unique identifier of the container reqif document
/// </summary>
private string reqifIdentifier;

protected override void OnParametersSet()
{
var attributeDefinition = this.AttributeDefinitions.FirstOrDefault();
Expand All @@ -65,7 +66,7 @@
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<AttributeDefinitionsComponent>().Error(e, "OnParametersSet Failed");
}
}
}
5 changes: 3 additions & 2 deletions reqifviewer/Components/AttributeValuesComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@using ReqIFSharp;
@using System.Text
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using Serilog

<RadzenFieldset AllowCollapse="true" Style="margin-bottom: 10px;">

Expand Down Expand Up @@ -124,9 +125,9 @@
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<AttributeValuesComponent>().Error(e, "QueryAttributeDefinitionUrl Failed");
}

return result;
}
}
}
4 changes: 2 additions & 2 deletions reqifviewer/Components/SpecObjectComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

@using System.Globalization
@using System.Text

@using ReqIFSharp
@using Serilog

<RadzenFieldset AllowCollapse="true" Style="margin-bottom: 10px;">

Expand Down Expand Up @@ -129,7 +129,7 @@
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<SpecObjectComponent>().Error(e, "QuerySpecObjectTypeUrl Failed");
}

return result;
Expand Down
3 changes: 2 additions & 1 deletion reqifviewer/Components/SpecificationsComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqIFSharp
@using Serilog

<RadzenDataGrid PageSize="10" AllowPaging="true" AllowSorting="true" AllowFiltering="true" AllowColumnResize="true" Data="@this.Specifications" TItem="Specification">
<Columns>
Expand Down Expand Up @@ -60,7 +61,7 @@
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<SpecificationsComponent>().Error(e, "OnParametersSet Failed");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
@page "/reqif/{ReqIF}/spectype/{SpecType}/attributedefinition/{Identifier}"

@using System.Globalization

@using ReqIFSharp
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using Serilog;

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -143,7 +143,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<AttributeDefinitionDetailsPage>().Error(e, "QuerySpecObjectTypeUrl Failed");
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@using ReqIFSharp
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using Serilog;

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -198,7 +199,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<DataTypeDefinitionDetailsPage>().Error(e, "LoadData Failed");
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@using ReqIFSharp
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using Serilog

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -114,7 +115,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<DataTypeDefinitionPage>().Error(e, "LoadData Failed");
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using System.Threading
@using Serilog

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -80,7 +81,7 @@ else

await this.LoadData();
}

/// <summary>
/// Load the data into view-models
/// </summary>
Expand All @@ -102,7 +103,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<ExternalObjectDetailsPage>().Error(e, "LoadData Failed");
}
finally
{
Expand All @@ -121,7 +122,5 @@ else
}

return "";

}

}
3 changes: 2 additions & 1 deletion reqifviewer/Pages/ExternalObjects/ExternalObjectPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
@using ReqIFSharp
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using Serilog

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -110,7 +111,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<ExternalObjectPage>().Error(e, "LoadData Failed");
}
finally
{
Expand Down
13 changes: 9 additions & 4 deletions reqifviewer/Pages/Index/IndexPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
@using System.Threading
@using ReqIFSharp
@using ReqifViewer.Infrastructure.Services
@using Serilog;

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -113,10 +114,14 @@ else
if (this.reqIfLoaderService.ReqIFData == null || !this.reqIfLoaderService.ReqIFData.Any())
{
this.reqIfs = null;

Log.ForContext<IndexPage>().Debug("no ReqIF loaded");
}
else
{
this.reqIfs = this.reqIfLoaderService.ReqIFData;

Log.ForContext<IndexPage>().Debug("a Total of {amount} ReqIF loaded", this.reqIfs.Count());
}
}

Expand All @@ -143,7 +148,7 @@ else

this.reqifisAvailable = true;

Console.WriteLine($"IndexPage: file read into stream in {sw.ElapsedMilliseconds} [ms]");
Log.ForContext<IndexPage>().Information("file read into stream in {time} [ms]" , sw.ElapsedMilliseconds);
}

/// <summary>
Expand Down Expand Up @@ -176,15 +181,15 @@ else
await this.reqIfLoaderService.Load(this.reqifStream, this.cancellationTokenSource.Token);
this.reqIfs = this.reqIfLoaderService.ReqIFData;

Console.WriteLine($"IndexPage: a total of {this.reqIfs.Count()} ReqIF objects deserialized in {sw.ElapsedMilliseconds} [ms]");
Log.ForContext<IndexPage>().Information("a total of {amount} ReqIF objects deserialized in {time} [ms]", this.reqIfs.Count(), sw.ElapsedMilliseconds);
}
catch (TaskCanceledException)
{
Console.WriteLine("IndexPage: Load was cancelled");
Log.ForContext<IndexPage>().Information("Load was cancelled");
}
catch (Exception e)
{
Console.WriteLine($"IndexPage: {e}");
Log.ForContext<IndexPage>().Error(e, "load reqif failed");
}
finally
{
Expand Down
3 changes: 2 additions & 1 deletion reqifviewer/Pages/ReqIF/ReqIFStatistics.razor
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@using ReqIFSharp
@using ReqifViewer.Infrastructure.ReqIFExtensions
@using ReqifViewer.Infrastructure.Services
@using Serilog

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -280,7 +281,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<ReqIFStatistics>().Error(e, "OnParametersSet Failed");
}
finally
{
Expand Down
3 changes: 2 additions & 1 deletion reqifviewer/Pages/SpecObject/SpecObjectDetailsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@using ReqifViewer.Infrastructure.Services
@using reqifviewer.Components
@using System.Threading
@using Serilog

@inject IReqIFLoaderService reqIfLoaderService

Expand Down Expand Up @@ -184,7 +185,7 @@ else
}
catch (Exception e)
{
Console.WriteLine(e);
Log.ForContext<SpecObjectDetailsPage>().Error(e, "LoadData Failed");
}
finally
{
Expand Down
Loading

0 comments on commit 02fa5ef

Please sign in to comment.