Skip to content

Commit

Permalink
feat(Logging): Example logging demonstration.
Browse files Browse the repository at this point in the history
  • Loading branch information
frostaura committed Jan 27, 2024
1 parent 52a84fa commit b7a7365
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Semantic.Core.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
.GetThoughtByName<LanguageModelThoughts>(nameof(LanguageModelThoughts));
var token = CancellationToken.None;

await services
.GetRequiredService<Test1>()
.UpdateAsync();


Console.Write("Question / Query: ");

var query = Console.ReadLine();
Expand Down
39 changes: 39 additions & 0 deletions src/Semantic.Core.Examples/Test1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;

namespace Semantic.Core.Examples;

public class Test1
{
private readonly Test2 _test2;
private readonly ILogger _logger;

public Test1(Test2 test2, ILogger<Test1> logger)
{
_logger = logger;
LogSemanticInformation($"Creating class {GetType().Name}.");
_test2 = test2;
}

public async Task UpdateAsync()
{
using (BeginSemanticScope($"Scope Name Update {GetType().Name}"))
{
LogSemanticInformation($"Initializing update on {GetType().Name}...");
await _test2.UpdateAsync();
LogSemanticInformation("Update done!");
}
}

private IDisposable BeginSemanticScope(string groupDescription)
{
return _logger.BeginScope(new KeyValuePair<string, object>(groupDescription, this));
}

private void LogSemanticInformation(string message)
{
var operationId = RuntimeHelpers.GetHashCode(this);

_logger.LogInformation($"[[[{operationId}]]]{message}");
}
}
41 changes: 41 additions & 0 deletions src/Semantic.Core.Examples/Test2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Runtime.CompilerServices;
using FrostAura.Libraries.Core.Extensions.Validation;
using Microsoft.Extensions.Logging;

namespace Semantic.Core.Examples;

public class Test2
{
private readonly Test3 _test3;
private readonly ILogger _logger;

public Test2(Test3 test3, ILogger<Test2> logger)
{
_logger = logger;
_test3 = test3.ThrowIfNull(nameof(test3));
}

public async Task UpdateAsync()
{
using (BeginSemanticScope($"Scope Name Update {GetType().Name}"))
{
LogSemanticInformation($"Initializing update on {GetType().Name}...");
await _test3.Update();
LogSemanticInformation($"Again, update on {GetType().Name}...");
await _test3.Update();
LogSemanticInformation("Update done!");
}
}

private IDisposable BeginSemanticScope(string groupDescription)
{
return _logger.BeginScope(new KeyValuePair<string, object>(groupDescription, this));
}

private void LogSemanticInformation(string message)
{
var operationId = RuntimeHelpers.GetHashCode(this);

_logger.LogInformation($"[[[{operationId}]]]{message}");
}
}
40 changes: 40 additions & 0 deletions src/Semantic.Core.Examples/Test3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Runtime.CompilerServices;
using Microsoft.Extensions.Logging;

namespace Semantic.Core.Examples;

public class Test3
{
private readonly ILogger _logger;

public Test3(ILogger<Test3> logger)
{
_logger = logger;
}

public async Task Update()
{
using (BeginSemanticScope($"Scope Name Update {GetType().Name}"))
{
LogSemanticInformation("Ping");
await Task.Delay(TimeSpan.FromSeconds(1));
LogSemanticInformation("Pong");
await Task.Delay(TimeSpan.FromSeconds(1));
LogSemanticInformation("Pang");
await Task.Delay(TimeSpan.FromSeconds(1));
LogSemanticInformation("Pung");
}
}

private IDisposable BeginSemanticScope(string groupDescription)
{
return _logger.BeginScope(new KeyValuePair<string, object>(groupDescription, this));
}

private void LogSemanticInformation(string message)
{
var operationId = RuntimeHelpers.GetHashCode(this);

_logger.LogInformation($"[[[{operationId}]]]{message}");
}
}

0 comments on commit b7a7365

Please sign in to comment.