-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2935 from umbraco/temp8-MOAR-LOGGING
MOAR Logging for V8
- Loading branch information
Showing
16 changed files
with
209 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Umbraco.Core/Logging/Serilog/Enrichers/HttpRequestIdEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
using System.Web; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Umbraco.Core.Logging.Serilog.Enrichers | ||
{ | ||
/// <summary> | ||
/// Enrich log events with a HttpRequestId GUID. | ||
/// Original source - https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestIdEnricher.cs | ||
/// Nupkg: 'Serilog.Web.Classic' contains handlers & extra bits we do not want | ||
/// </summary> | ||
internal class HttpRequestIdEnricher : ILogEventEnricher | ||
{ | ||
/// <summary> | ||
/// The property name added to enriched log events. | ||
/// </summary> | ||
public const string HttpRequestIdPropertyName = "HttpRequestId"; | ||
|
||
static readonly string RequestIdItemName = typeof(HttpRequestIdEnricher).Name + "+RequestId"; | ||
|
||
/// <summary> | ||
/// Enrich the log event with an id assigned to the currently-executing HTTP request, if any. | ||
/// </summary> | ||
/// <param name="logEvent">The log event to enrich.</param> | ||
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param> | ||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (logEvent == null) throw new ArgumentNullException("logEvent"); | ||
|
||
Guid requestId; | ||
if (!TryGetCurrentHttpRequestId(out requestId)) | ||
return; | ||
|
||
var requestIdProperty = new LogEventProperty(HttpRequestIdPropertyName, new ScalarValue(requestId)); | ||
logEvent.AddPropertyIfAbsent(requestIdProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Retrieve the id assigned to the currently-executing HTTP request, if any. | ||
/// </summary> | ||
/// <param name="requestId">The request id.</param> | ||
/// <returns><c>true</c> if there is a request in progress; <c>false</c> otherwise.</returns> | ||
public static bool TryGetCurrentHttpRequestId(out Guid requestId) | ||
{ | ||
if (HttpContext.Current == null) | ||
{ | ||
requestId = default(Guid); | ||
return false; | ||
} | ||
|
||
var requestIdItem = HttpContext.Current.Items[RequestIdItemName]; | ||
if (requestIdItem == null) | ||
HttpContext.Current.Items[RequestIdItemName] = requestId = Guid.NewGuid(); | ||
else | ||
requestId = (Guid)requestIdItem; | ||
|
||
return true; | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/Umbraco.Core/Logging/Serilog/Enrichers/HttpRequestNumberEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Web; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Umbraco.Core.Logging.Serilog.Enrichers | ||
{ | ||
/// <summary> | ||
/// Enrich log events with a HttpRequestNumber unique within the current | ||
/// logging session. | ||
/// Original source - https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpRequestNumberEnricher.cs | ||
/// Nupkg: 'Serilog.Web.Classic' contains handlers & extra bits we do not want | ||
/// </summary> | ||
internal class HttpRequestNumberEnricher : ILogEventEnricher | ||
{ | ||
/// <summary> | ||
/// The property name added to enriched log events. | ||
/// </summary> | ||
public const string HttpRequestNumberPropertyName = "HttpRequestNumber"; | ||
|
||
static int _lastRequestNumber; | ||
static readonly string RequestNumberItemName = typeof(HttpRequestNumberEnricher).Name + "+RequestNumber"; | ||
|
||
/// <summary> | ||
/// Enrich the log event with the number assigned to the currently-executing HTTP request, if any. | ||
/// </summary> | ||
/// <param name="logEvent">The log event to enrich.</param> | ||
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param> | ||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (logEvent == null) throw new ArgumentNullException("logEvent"); | ||
|
||
if (HttpContext.Current == null) | ||
return; | ||
|
||
int requestNumber; | ||
var requestNumberItem = HttpContext.Current.Items[RequestNumberItemName]; | ||
if (requestNumberItem == null) | ||
HttpContext.Current.Items[RequestNumberItemName] = requestNumber = Interlocked.Increment(ref _lastRequestNumber); | ||
else | ||
requestNumber = (int)requestNumberItem; | ||
|
||
var requestNumberProperty = new LogEventProperty(HttpRequestNumberPropertyName, new ScalarValue(requestNumber)); | ||
logEvent.AddPropertyIfAbsent(requestNumberProperty); | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Umbraco.Core/Logging/Serilog/Enrichers/HttpSessionIdEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
using System; | ||
using System.Web; | ||
|
||
namespace Umbraco.Core.Logging.Serilog.Enrichers | ||
{ | ||
/// <summary> | ||
/// Enrich log events with the HttpSessionId property. | ||
/// Original source - https://github.com/serilog-web/classic/blob/master/src/SerilogWeb.Classic/Classic/Enrichers/HttpSessionIdEnricher.cs | ||
/// Nupkg: 'Serilog.Web.Classic' contains handlers & extra bits we do not want | ||
/// </summary> | ||
internal class HttpSessionIdEnricher : ILogEventEnricher | ||
{ | ||
/// <summary> | ||
/// The property name added to enriched log events. | ||
/// </summary> | ||
public const string HttpSessionIdPropertyName = "HttpSessionId"; | ||
|
||
/// <summary> | ||
/// Enrich the log event with the current ASP.NET session id, if sessions are enabled.</summary> | ||
/// <param name="logEvent">The log event to enrich.</param> | ||
/// <param name="propertyFactory">Factory for creating new properties to add to the event.</param> | ||
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory) | ||
{ | ||
if (logEvent == null) throw new ArgumentNullException("logEvent"); | ||
|
||
if (HttpContext.Current == null) | ||
return; | ||
|
||
if (HttpContext.Current.Session == null) | ||
return; | ||
|
||
var sessionId = HttpContext.Current.Session.SessionID; | ||
var sessionIdProperty = new LogEventProperty(HttpSessionIdPropertyName, new ScalarValue(sessionId)); | ||
logEvent.AddPropertyIfAbsent(sessionIdProperty); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...ing/Serilog/Log4NetLevelMapperEnricher.cs → ...g/Enrichers/Log4NetLevelMapperEnricher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.