Skip to content

Commit 20a06d2

Browse files
niemyjskiyuefengkaiBrook
authored
Feature/add user info description (#11)
* Add user info/description (#10) * SerilogProperty-AddUserInfo * update README and Version * change * change Co-authored-by: Brook <[email protected]> * Added examples for #10 and changes for perf Co-authored-by: Zengzhi Gao <[email protected]> Co-authored-by: Brook <[email protected]>
1 parent 14690d6 commit 20a06d2

File tree

5 files changed

+70
-8
lines changed

5 files changed

+70
-8
lines changed

sample/SampleWeb/Controllers/ValuesController.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
3+
using Exceptionless.Models;
4+
using Exceptionless.Models.Data;
25
using Microsoft.AspNetCore.Mvc;
36
using Microsoft.Extensions.Logging;
7+
using Serilog.Context;
48

59
namespace SampleWeb.Controllers
610
{
@@ -18,7 +22,31 @@ public ValuesController(ILogger<ValuesController> logger)
1822
[HttpGet]
1923
public string Get()
2024
{
21-
_logger.LogInformation("Get was called");
25+
_logger.LogInformation("Get was called");
26+
return $"[{Activity.Current?.Id}] {User.Identity?.Name}";
27+
}
28+
29+
[HttpGet("advanced-topic-user")]
30+
public string AdvancedTopicUser()
31+
{
32+
// This call is is authenticated so a user identity would automatically be set.
33+
// However we are overriding it with our own custom user. You may want to do this
34+
// in a microservice where you know the user but you may not be authenticated.
35+
using (LogContext.PushProperty(Event.KnownDataKeys.UserInfo, new UserInfo(User.Identity?.Name + " Custom", "Test User Full Name"), true)) {
36+
_logger.LogInformation("This log event will have a custom user set.");
37+
}
38+
39+
return $"[{Activity.Current?.Id}] {User.Identity?.Name}";
40+
}
41+
42+
[HttpGet("advanced-topic-user-description")]
43+
public string AdvancedTopicUserDescription(string description)
44+
{
45+
// User descriptions was intended to provide a description from an end user why an error happened.
46+
using (LogContext.PushProperty(Event.KnownDataKeys.UserDescription, new UserDescription(User.Identity?.Name, description), true)) {
47+
_logger.LogError(new Exception("Test"), "This error event will have a user description set on it.");
48+
}
49+
2250
return $"[{Activity.Current?.Id}] {User.Identity?.Name}";
2351
}
2452
}

sample/SampleWeb/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4040
app.UseRouting();
4141
app.UseAuthentication();
4242
app.UseAuthorization();
43-
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
43+
app.UseEndpoints(endpoints => endpoints.MapControllers());
4444
}
4545
}
4646
}

src/Serilog.Sinks.Exceptionless/Serilog.Sinks.Exceptionless.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Description>Exceptionless sink for Serilog</Description>
5-
<VersionPrefix>3.1.2</VersionPrefix>
5+
<VersionPrefix>3.1.3</VersionPrefix>
66
<Authors>Serilog Contributors</Authors>
77
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
88
<NoWarn>$(NoWarn);CS1591</NoWarn>

src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessClientExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
using Serilog.Core;
66
using Serilog.Events;
77

8-
namespace Serilog.Sinks.Exceptionless {
8+
namespace Serilog.Sinks.Exceptionless
9+
{
910
public static class ExceptionlessClientExtensions {
1011
public static EventBuilder CreateFromLogEvent(this ExceptionlessClient client, LogEvent log) {
1112
string message = log.RenderMessage();

src/Serilog.Sinks.Exceptionless/Sinks/Exceptionless/ExceptionlessSink.cs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
23
using Exceptionless;
34
using Exceptionless.Dependency;
45
using Exceptionless.Logging;
6+
using Exceptionless.Models;
7+
using Exceptionless.Models.Data;
58
using Serilog.Core;
69
using Serilog.Events;
710

@@ -87,9 +90,39 @@ public void Emit(LogEvent logEvent) {
8790
var builder = _client.CreateFromLogEvent(logEvent);
8891

8992
if (_includeProperties && logEvent.Properties != null) {
90-
foreach (var property in logEvent.Properties)
91-
if (property.Key != Constants.SourceContextPropertyName)
92-
builder.SetProperty(property.Key, property.Value.FlattenProperties());
93+
foreach (var prop in logEvent.Properties)
94+
{
95+
switch (prop.Key)
96+
{
97+
case Constants.SourceContextPropertyName:
98+
continue;
99+
case Event.KnownDataKeys.UserInfo when prop.Value is StructureValue uis && String.Equals(nameof(UserInfo), uis.TypeTag):
100+
var userInfo = uis.FlattenProperties() as Dictionary<string, object>;
101+
if (userInfo is null)
102+
continue;
103+
104+
// UserDescription Data property is currently ignored.
105+
string identity = userInfo[nameof(UserInfo.Identity)] as string;
106+
string name = userInfo[nameof(UserInfo.Name)] as string;
107+
if (!String.IsNullOrWhiteSpace(identity) || !String.IsNullOrWhiteSpace(name))
108+
builder.SetUserIdentity(identity, name);
109+
break;
110+
case Event.KnownDataKeys.UserDescription when prop.Value is StructureValue uds && String.Equals(nameof(UserDescription), uds.TypeTag):
111+
var userDescription = uds.FlattenProperties() as Dictionary<string, object>;
112+
if (userDescription is null)
113+
continue;
114+
115+
// UserDescription Data property is currently ignored.
116+
string emailAddress = userDescription[nameof(UserDescription.EmailAddress)] as string;
117+
string description = userDescription[nameof(UserDescription.Description)] as string;
118+
if (!String.IsNullOrWhiteSpace(emailAddress) || !String.IsNullOrWhiteSpace(description))
119+
builder.SetUserDescription(emailAddress, description);
120+
break;
121+
default:
122+
builder.SetProperty(prop.Key, prop.Value.FlattenProperties());
123+
break;
124+
}
125+
}
93126
}
94127

95128
_additionalOperation?.Invoke(builder);

0 commit comments

Comments
 (0)