Skip to content

Instantly share code, notes, and snippets.

@chivandikwa
Created June 8, 2019 21:23
Show Gist options
  • Save chivandikwa/ed1443d2c428d4b9a96517c9b65817c1 to your computer and use it in GitHub Desktop.
Save chivandikwa/ed1443d2c428d4b9a96517c9b65817c1 to your computer and use it in GitHub Desktop.
Microsoft bot framework activity logging middleware
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Serilog;
namespace CoreBot.Middleware
{
public class LoggingMiddleware : IMiddleware
{
private readonly ILogger _logger;
public LoggingMiddleware(ILogger logger)
{
_logger = logger;
}
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next,
CancellationToken cancellationToken = new CancellationToken())
{
_logger.ForContext("activity", turnContext.Activity, true)
.Verbose($"{turnContext.Activity.Type} received");
turnContext.OnSendActivities(async (context, activities, nextSend) =>
{
_logger.ForContext("activity", context.Activity, true)
.Verbose($"{context.Activity.Type} received");
return await nextSend();
});
turnContext.OnUpdateActivity(async (context, activity, nextUpdate) =>
{
_logger.ForContext("activity", context.Activity, true)
.Verbose($"{context.Activity.Type} received");
return await nextUpdate();
});
turnContext.OnDeleteActivity(async (context, reference, nextDelete) =>
{
_logger.ForContext("activity", context.Activity, true)
.Verbose($"{context.Activity.Type} received");
await nextDelete();
});
await next(cancellationToken).ConfigureAwait(false);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment