Skip to content

Commit

Permalink
Add EnvironmentUserNameEnricher + unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
augustoproiete committed Nov 7, 2015
1 parent 599e643 commit 14c070b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/Serilog.FullNetFx/Enrichers/EnvironmentUserNameEnricher.cs
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;
}
}
}
12 changes: 12 additions & 0 deletions src/Serilog.FullNetFx/LoggerConfigurationFullNetFxExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,18 @@ public static LoggerConfiguration WithMachineName(
return enrichmentConfiguration.With<MachineNameEnricher>();
}

/// <summary>
/// Enriches log events with an EnvironmentUserName property containing [<see cref="Environment.UserDomainName"/>\]<see cref="Environment.UserName"/>.
/// </summary>
/// <param name="enrichmentConfiguration">Logger enrichment configuration.</param>
/// <returns>Configuration object allowing method chaining.</returns>
public static LoggerConfiguration WithEnvironmentUserName(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null) throw new ArgumentNullException("enrichmentConfiguration");
return enrichmentConfiguration.With<EnvironmentUserNameEnricher>();
}

/// <summary>
/// Reads the &lt;appSettings&gt; element of App.config or Web.config, searching for for keys
/// that look like: <code>serilog:*</code>, which are used to configure
Expand Down
1 change: 1 addition & 0 deletions src/Serilog.FullNetFx/Serilog.FullNetFx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<ItemGroup>
<Compile Include="Context\ImmutableStack.cs" />
<Compile Include="Enrichers\LogContextEnricher.cs" />
<Compile Include="Enrichers\EnvironmentUserNameEnricher.cs" />
<Compile Include="Enrichers\ProcessIdEnricher.cs" />
<Compile Include="Enrichers\MachineNameEnricher.cs" />
<Compile Include="Enrichers\ThreadIdEnricher.cs" />
Expand Down
25 changes: 25 additions & 0 deletions test/Serilog.Tests/Enrichers/EnvironmentUserNameEnricherTests.cs
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());
}
}
}
1 change: 1 addition & 0 deletions test/Serilog.Tests/Serilog.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="Core\LoggerTests.cs" />
<Compile Include="Core\SafeAggregateSinkTests.cs" />
<Compile Include="Core\MessageTemplateTests.cs" />
<Compile Include="Enrichers\EnvironmentUserNameEnricherTests.cs" />
<Compile Include="Events\DictionaryValueTests.cs" />
<Compile Include="Events\LogEventPropertyValueTests.cs" />
<Compile Include="Filters\MatchingTests.cs" />
Expand Down

0 comments on commit 14c070b

Please sign in to comment.