Skip to content

Commit 3731bcb

Browse files
Define EventKey as readonly record struct.
1 parent b68c884 commit 3731bcb

File tree

2 files changed

+10
-51
lines changed

2 files changed

+10
-51
lines changed

samples/Sample/Program.cs

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,16 @@
3232
var serviceProvider = services.BuildServiceProvider();
3333
var logger = serviceProvider.GetRequiredService<ILogger<Program>>();
3434

35-
var startTime = DateTimeOffset.UtcNow;
36-
logger.LogInformation(1, "Started at {StartTime} and 0x{Hello:X} is hex of 42", startTime, 42);
35+
var eventId = new EventId(1001, "Test");
3736

38-
try
37+
for (int i = 0; i < 1_000; i++)
3938
{
40-
throw new Exception("Boom!");
39+
logger.Log(
40+
LogLevel.Information,
41+
eventId,
42+
"Subscription {SubscriptionId} for entity {EntityName} handler for message {MessageId} has been successfully completed.",
43+
"my-subscription-id",
44+
"TestQueue",
45+
1);
4146
}
42-
catch (Exception ex)
43-
{
44-
logger.LogCritical(ex, "Unexpected critical error starting application");
45-
logger.Log(LogLevel.Critical, 0, "Unexpected critical error", ex, null!);
46-
// This write should not log anything
47-
logger.Log<object>(LogLevel.Critical, 0, null!, null, null!);
48-
logger.LogError(ex, "Unexpected error");
49-
logger.LogWarning(ex, "Unexpected warning");
50-
}
51-
52-
using (logger.BeginScope("Main"))
53-
{
54-
logger.LogInformation("Waiting for user input");
55-
var key = Console.Read();
56-
logger.LogInformation("User pressed {@KeyInfo}", new { Key = key, KeyChar = (char)key });
57-
}
58-
59-
var endTime = DateTimeOffset.UtcNow;
60-
logger.LogInformation(2, "Stopping at {StopTime}", endTime);
61-
62-
logger.LogInformation("Stopping");
63-
64-
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "RESULT", "START TIME", "END TIME", "DURATION(ms)");
65-
logger.LogInformation("{Result,-10:l}{StartTime,15:l}{EndTime,15:l}{Duration,15:l}", "------", "----- ----", "--- ----", "------------");
66-
logger.LogInformation("{Result,-10:l}{StartTime,15:mm:s tt}{EndTime,15:mm:s tt}{Duration,15}", "SUCCESS", startTime, endTime, (endTime - startTime).TotalMilliseconds);
67-
6847
serviceProvider.Dispose();

src/Serilog.Extensions.Logging/Extensions/Logging/EventIdPropertyCache.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static LogEventProperty CreateCore(in EventKey eventKey)
6666
return new LogEventProperty("EventId", new StructureValue(properties));
6767
}
6868

69-
private readonly struct EventKey : IEquatable<EventKey>
69+
private readonly record struct EventKey
7070
{
7171

7272
public EventKey(EventId eventId)
@@ -78,26 +78,6 @@ public EventKey(EventId eventId)
7878
public int Id { get; }
7979

8080
public string? Name { get; }
81-
82-
/// <inheritdoc />
83-
public override int GetHashCode()
84-
{
85-
unchecked
86-
{
87-
var hashCode = 17;
88-
89-
hashCode = (hashCode * 397) ^ this.Id;
90-
hashCode = (hashCode * 397) ^ (this.Name?.GetHashCode() ?? 0);
91-
92-
return hashCode;
93-
}
94-
}
95-
96-
/// <inheritdoc />
97-
public bool Equals(EventKey other) => this.Id == other.Id && this.Name == other.Name;
98-
99-
/// <inheritdoc />
100-
public override bool Equals(object? obj) => obj is EventKey other && Equals(other);
10181
}
10282
}
10383
}

0 commit comments

Comments
 (0)