forked from postsharp/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSampledLoggingActionFilter.cs
More file actions
48 lines (41 loc) · 1.12 KB
/
Copy pathSampledLoggingActionFilter.cs
File metadata and controls
48 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Microsoft.AspNetCore.Mvc.Filters;
using PostSharp.Patterns.Diagnostics;
using System;
using System.Threading.Tasks;
namespace MicroserviceExample
{
[Log(AttributeExclude = true)]
public class SampledLoggingActionFilter : IAsyncActionFilter
{
private static readonly Random random = new Random();
private static LoggingVerbosityConfiguration verbosityManager;
public static void Initialize(LoggingBackend backend)
{
verbosityManager = backend.CreateVerbosityConfiguration();
// Verbosity is High (Debug level) by default.
}
public static bool IsInitialized => verbosityManager != null;
private static bool IsLogged(ActionExecutingContext context)
{
lock (random)
{
// Log 10% of requests.
return random.NextDouble() < 0.1;
}
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (IsInitialized && IsLogged(context))
{
using (verbosityManager.Use())
{
await next();
}
}
else
{
await next();
}
}
}
}