forked from serilog/serilog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EnvironmentUserNameEnricher + unit test
- Loading branch information
1 parent
599e643
commit 14c070b
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/Serilog.FullNetFx/Enrichers/EnvironmentUserNameEnricher.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,58 @@ | ||
// Copyright 2013-2015 Serilog Contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using System; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Serilog.Enrichers | ||
{ | ||
/// <summary> | ||
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>. | ||
/// </summary> | ||
public class EnvironmentUserNameEnricher : ILogEventEnricher | ||
{ | ||
LogEventProperty _cachedProperty; | ||
|
||
/// <summary> | ||
/// The property name added to enriched log events. | ||
/// </summary> | ||
public const string EnvironmentUserNamePropertyName = "EnvironmentUserName"; | ||
|
||
/// <summary> | ||
/// Enrich the log event. | ||
/// </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) | ||
{ | ||
_cachedProperty = _cachedProperty ?? propertyFactory.CreateProperty(EnvironmentUserNamePropertyName, GetEnvironmentUserName()); | ||
logEvent.AddPropertyIfAbsent(_cachedProperty); | ||
} | ||
|
||
private static string GetEnvironmentUserName() | ||
{ | ||
#if !ASPNETCORE50 | ||
var userDomainName = Environment.UserDomainName; | ||
var userName = Environment.UserName; | ||
#else | ||
var userDomainName = Environment.GetEnvironmentVariable("USERNAME"); | ||
var userName = Environment.GetEnvironmentVariable("USERDOMAIN"); | ||
#endif | ||
return !string.IsNullOrWhiteSpace(userDomainName) | ||
? string.Format(@"{0}\{1}", userDomainName, userName) | ||
: userName; | ||
} | ||
} | ||
} |
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
25 changes: 25 additions & 0 deletions
25
test/Serilog.Tests/Enrichers/EnvironmentUserNameEnricherTests.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,25 @@ | ||
using NUnit.Framework; | ||
using Serilog.Events; | ||
using Serilog.Tests.Support; | ||
|
||
namespace Serilog.Tests.Enrichers | ||
{ | ||
[TestFixture] | ||
public class EnvironmentUserNameEnricherTests | ||
{ | ||
[Test] | ||
public void EnvironmentUserNameEnricherIsApplied() | ||
{ | ||
LogEvent evt = null; | ||
var log = new LoggerConfiguration() | ||
.Enrich.WithEnvironmentUserName() | ||
.WriteTo.Sink(new DelegatingSink(e => evt = e)) | ||
.CreateLogger(); | ||
|
||
log.Information(@"Has an EnvironmentUserName property with [domain\]userName"); | ||
|
||
Assert.IsNotNull(evt); | ||
Assert.IsNotNullOrEmpty((string)evt.Properties["EnvironmentUserName"].LiteralValue()); | ||
} | ||
} | ||
} |
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