Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
new helper methods which extend ILogger
Browse files Browse the repository at this point in the history
- helper methods meant to reduce the ForContext and message template code for specifying the event type and subject
  • Loading branch information
sirkirby committed Sep 20, 2017
1 parent 69de514 commit 31b3ca3
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,23 @@ Log.ForContext("EventPropertyName", "myCustomType").Information("{@OtherData}",

Specify the Serilog logging level. Default is `LogEventLevel.Information`

### Helper Extensions

The sink adds additional extension methods to `ILogger` which can help simplify your Event Grid logging code:

```csharp
using Serilog.Sinks.EventGrid
```

```csharp
// type, subject, and information event message with properties
Log.EventGrid("myEventTypeName", "myEventSubjectName", "This is my Event {@MyContext}", myContext);
// type and information event message with properties
Log.EventGrid("myEventTypeName", "This is my Event {@MyContext}", myContext);
// type and information event message
Log.EventGrid("myEventTypeName", "This is my Event");
```

### Custom Attributes

As an alternative to specifying the subject and type through log configuration or properties, you can decorate your code at design time with the `[EventGridSubject]` and `[EventGridType]` Attributes. Any method or class is supported, using one or both on each. The Serilog log event called within the context of a method or class decorated with either attribute, will use those values when submitting the event. The first ones closest to the log event call in the stack, win.
Expand Down
31 changes: 31 additions & 0 deletions src/Serilog.Sinks.EventGrid/LoggerEventGridExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Serilog.Sinks.EventGrid
{
public static class LoggerEventGridExtensions
{
/// <summary>Information log event helper for EventGrid sink</summary>
/// <param name="logger">The Serilog ILogger</param>
/// <param name="eventType">The event type sent to EventGrid Grid</param>
/// <param name="subject">The event subject sent to EventGrid Grid</param>
/// <param name="messageTemplate">The Serilog logger message template</param>
/// <param name="props">The values references in the templace to be added to the EventGrid Grid data payload</param>
public static void EventGrid(this ILogger logger, string eventType, string subject, string messageTemplate, params object[] props)
{
if (!string.IsNullOrEmpty(subject))
logger = logger.ForContext("EventSubject", subject);
if (!string.IsNullOrEmpty(eventType))
logger = logger.ForContext("EventType", eventType);
if (!string.IsNullOrEmpty(messageTemplate))
logger.Information(messageTemplate, props);
}

public static void EventGrid(this ILogger logger, string eventType, string messageTemplate, params object[] props)
{
logger.EventGrid(eventType, null, messageTemplate, props);
}

public static void EventGrid(this ILogger logger, string eventType, string messageTemplate)
{
logger.EventGrid(eventType, null, messageTemplate, null);
}
}
}

0 comments on commit 31b3ca3

Please sign in to comment.