-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Release/211 add basic properties to message (#222)
* Add SendMessageEvents class for setting message properties prior Publish Rename - RouteKey -> RoutingKey - Period -> BufferingTimeLimit
- Loading branch information
Showing
29 changed files
with
428 additions
and
161 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
65 changes: 65 additions & 0 deletions
65
samples/Net8AppsettingsJsonSample/CustomSendMessageEvents.cs
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,65 @@ | ||
// Copyright 2015-2024 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using RabbitMQ.Client; | ||
using Serilog.Events; | ||
using Serilog.Sinks.RabbitMQ; | ||
|
||
namespace Net8AppsettingsJsonSample; | ||
|
||
/// <summary> | ||
/// A custom SendMessageEvents class to handle events before sending a message. | ||
/// </summary> | ||
public sealed class CustomSendMessageEvents : ISendMessageEvents | ||
{ | ||
private readonly string _defaultRoutingKey; | ||
|
||
/// <summary> | ||
/// The constructor for the CustomSendMessageEvents class. | ||
/// </summary> | ||
/// <param name="defaultRoutingKey">The default routing key.</param> | ||
public CustomSendMessageEvents(string defaultRoutingKey) | ||
{ | ||
_defaultRoutingKey = defaultRoutingKey; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void OnSetMessageProperties(LogEvent logEvent, IBasicProperties properties) | ||
{ | ||
// example of setting message headers based on log event properties | ||
logEvent.Properties.TryGetValue("messageType", out var messageType); | ||
properties.Headers = new Dictionary<string, object?> | ||
{ | ||
{ "messageType", messageType?.ToString() }, | ||
{ "log-level", logEvent.Level.ToString() }, | ||
}; | ||
|
||
// example of setting correlation id based on log event properties | ||
if (logEvent.Properties.TryGetValue(LogProperties.CORRELATION_ID, out var correlationId)) | ||
{ | ||
properties.CorrelationId = correlationId.ToString(); | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public string OnGetRoutingKey(LogEvent logEvent, string defaultRoutingKey) | ||
{ | ||
// example of routing based on log level | ||
return logEvent.Level switch | ||
{ | ||
LogEventLevel.Error => "error", | ||
_ => _defaultRoutingKey | ||
}; | ||
} | ||
} |
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,12 @@ | ||
namespace Net8AppsettingsJsonSample; | ||
|
||
/// <summary> | ||
/// A class to hold the log context properties. | ||
/// </summary> | ||
public static class LogProperties | ||
{ | ||
/// <summary> | ||
/// The correlation id property name. | ||
/// </summary> | ||
public const string CORRELATION_ID = "CorrelationId"; | ||
} |
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
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
Oops, something went wrong.