Skip to content

Commit 2628b22

Browse files
author
m-hoseinzadeh-a
committed
improve context info
1 parent a925aa7 commit 2628b22

8 files changed

Lines changed: 30 additions & 9 deletions

File tree

Olive.Log.EventBus/EventBusLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ public override void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, Ev
1919
if (exception != null) r.AppendLine(exception.ToLogString());
2020

2121
var contextInfo = Olive.Log.ContextProvider?.Invoke();
22-
if (contextInfo.HasValue()) r.AppendLine(contextInfo);
2322

24-
Provider.AddMessage(timestamp, r.ToString(), exception?.GetUsefulStack(), (int)logLevel);
23+
Provider.AddMessage(timestamp, r.ToString(), exception?.GetUsefulStack(), (int)logLevel, contextInfo);
2524
}
2625
}
2726
}

Olive.Log.EventBus/Olive.Log.EventBus.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
4-
<Version>8.0.1</Version>
4+
<Version>8.0.2</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

Olive/Logging/FileLogger/BatchingLogger.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ public virtual void Log<TState>(DateTimeOffset timestamp, LogLevel logLevel, Eve
3535
if (exception != null) r.AppendLine(exception.ToFullMessage());
3636

3737
var contextInfo = Olive.Log.ContextProvider?.Invoke();
38-
if (contextInfo.HasValue()) r.AppendLine(contextInfo);
3938

40-
Provider.AddMessage(timestamp, r.ToString(), exception?.GetUsefulStack(), (int)logLevel);
39+
Provider.AddMessage(timestamp, r.ToString(), exception?.GetUsefulStack(), (int)logLevel, contextInfo);
4140
}
4241

4342
public virtual void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)

Olive/Logging/FileLogger/BatchingLoggerProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ async Task ProcessLogQueue(object _)
6767
}
6868
}
6969

70-
public void AddMessage(DateTimeOffset timestamp, string message, string stack = null, int severity = 0)
70+
public void AddMessage(DateTimeOffset timestamp, string message, string stack = null, int severity = 0, string contextInfo = null)
7171
{
7272
if (!MessageQueue.IsAddingCompleted)
7373
{
@@ -76,6 +76,7 @@ public void AddMessage(DateTimeOffset timestamp, string message, string stack =
7676
MessageQueue.Add(new()
7777
{
7878
Message = message,
79+
ContextInfo = contextInfo,
7980
Timestamp = timestamp,
8081
Severity = severity,
8182
Stack = stack

Olive/Logging/FileLogger/FileLoggerProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ public override async Task WriteMessagesAsync(IEnumerable<LogMessage> messages,
3131
var file = Path.GetFile(FilePrefix + group.Key.ToString("yyyyMMdd") + ".txt");
3232
if (MaxFileSize > 0 && file.Exists() && file.Length > MaxFileSize) return;
3333

34-
await file.AppendAllTextAsync(group.Select(x => x.Message).ToLinesString()).ConfigureAwait(false);
34+
await file.AppendAllTextAsync(group.Select(x =>
35+
x.ContextInfo.HasValue() ? x.Message + x.ContextInfo + System.Environment.NewLine : x.Message
36+
).ToLinesString()).ConfigureAwait(false);
3537
}
3638
}
3739

Olive/Logging/FileLogger/LogMessage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public struct LogMessage
66
{
77
public DateTimeOffset Timestamp { get; set; }
88
public string Message { get; set; }
9+
public string ContextInfo { get; set; }
910
public string Stack { get; set; }
1011
public int Severity { set; get; }
1112
}

Olive/Logging/Log.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,29 +127,48 @@ static void InitDefaultContextProvider()
127127

128128
var contextType = httpContext.GetType();
129129

130+
// User info
130131
var user = contextType.GetProperty("User")?.GetValue(httpContext) as ClaimsPrincipal;
131132
var userId = user?.GetId();
133+
var userEmail = user?.GetEmail();
132134

135+
// Request info
133136
var request = contextType.GetProperty("Request")?.GetValue(httpContext);
134-
string requestUrl = null;
137+
string requestUrl = null, httpMethod = null, userAgent = null, traceId = null;
135138
if (request != null)
136139
{
137140
var reqType = request.GetType();
138141
var pathBase = reqType.GetProperty("PathBase")?.GetValue(request)?.ToString();
139142
var path = reqType.GetProperty("Path")?.GetValue(request)?.ToString();
140143
var queryString = reqType.GetProperty("QueryString")?.GetValue(request)?.ToString();
141144
requestUrl = $"{pathBase}{path}{queryString}";
145+
httpMethod = reqType.GetProperty("Method")?.GetValue(request)?.ToString();
146+
147+
var headers = reqType.GetProperty("Headers")?.GetValue(request);
148+
if (headers != null)
149+
{
150+
var indexer = headers.GetType().GetProperty("Item", new[] { typeof(string) });
151+
userAgent = indexer?.GetValue(headers, new object[] { "User-Agent" })?.ToString();
152+
}
142153
}
143154

155+
// Connection info
144156
var connection = contextType.GetProperty("Connection")?.GetValue(httpContext);
145157
var userIp = connection?.GetType().GetProperty("RemoteIpAddress")?.GetValue(connection)?.ToString();
146158

159+
// Trace identifier
160+
traceId = contextType.GetProperty("TraceIdentifier")?.GetValue(httpContext)?.ToString();
161+
147162
if (userId.IsEmpty() && requestUrl.IsEmpty() && userIp.IsEmpty()) return null;
148163

149164
var r = new StringBuilder();
150165
if (userId.HasValue()) r.AppendLine($" UserId: {userId}");
166+
if (userEmail.HasValue()) r.AppendLine($" UserEmail: {userEmail}");
167+
if (httpMethod.HasValue()) r.AppendLine($" HttpMethod: {httpMethod}");
151168
if (requestUrl.HasValue()) r.AppendLine($" RequestUrl: {requestUrl}");
152169
if (userIp.HasValue()) r.AppendLine($" UserIP: {userIp}");
170+
if (userAgent.HasValue()) r.AppendLine($" UserAgent: {userAgent}");
171+
if (traceId.HasValue()) r.AppendLine($" TraceId: {traceId}");
153172
return r.ToString().TrimEnd();
154173
}
155174
catch { return null; }

Olive/Olive.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFramework>netstandard2.0</TargetFramework>
4-
<Version>8.3.3</Version>
4+
<Version>8.3.4</Version>
55
<OutputType>Library</OutputType>
66
<LangVersion>latest</LangVersion>
77
<Authors>Geeks Ltd, UK</Authors>

0 commit comments

Comments
 (0)