-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Logging): Example logging demonstration.
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} |