Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .vs/NLog.Extensions/v15/sqlite3/storage.ide
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ namespace NLog.Extensions.AzureTableStorage.Tests
public class AzureTableStorageKeysTests : IDisposable
{
#region Constants

//must match table name in AzureTableStorage target in NLog.config
private const string TargetTableName = "AzureTableStorageKeysTests";
private const string TargetTableName = "AzureTableStorageKeysTests";

#endregion Constants

#region Fields

private readonly Logger _logger;
private readonly CloudTable _cloudTable;

#endregion Fields

#region Properties

#endregion Properties

#region Constructors

public AzureTableStorageKeysTests()
{
try
Expand All @@ -42,14 +48,67 @@ public AzureTableStorageKeysTests()
throw new Exception("Failed to initialize tests, make sure Azure Storage Emulator is running.", ex);
}
}

#endregion Constructors

#region Methods

[Fact]
public void IncludeGdcInPatitionKey()
{
var gdcValue = Guid.NewGuid().ToString().Replace("-", string.Empty);
GlobalDiagnosticsContext.Set("item", gdcValue);

// configure keys
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${gdc:item}__${guid}";
LogManager.ReconfigExistingLoggers();


// log something
_logger.Log(LogLevel.Info, "information");

var entity = GetLogEntities().First();
var key = entity.RowKey.Split("__".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Assert.Equal(2, key.Length);
Assert.Equal(gdcValue, key[0]);
}

[Fact]
public void IncludeMultipleGdcInPatitionKey()
{
var gdcVariables = new Dictionary<string, string>
{
{"item1", Guid.NewGuid().ToString().Replace("-", string.Empty)},
{"item2", Guid.NewGuid().ToString().Replace("-", string.Empty)}
};

foreach (var pair in gdcVariables)
{
GlobalDiagnosticsContext.Set(pair.Key, pair.Value);
}

// configure keys
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${gdc:item1}__${gdc:item2}__${guid}";
LogManager.ReconfigExistingLoggers();


// log something
_logger.Log(LogLevel.Info, "information");

var entity = GetLogEntities().First();
var key = entity.RowKey.Split("__".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
Assert.Equal(3, key.Length);
Assert.True(gdcVariables.ContainsValue(key[0]));
Assert.True(gdcVariables.ContainsValue(key[1]));
}

[Fact]
public void IncludeDateInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${date}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -68,7 +127,7 @@ public void IncludeDateInRowKey()
public void IncludeTimeAndGuidInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${time}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -87,7 +146,7 @@ public void IncludeTimeAndGuidInRowKey()
public void IncludeTicksAndLongDateInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${ticks}__${longdate}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -106,7 +165,7 @@ public void IncludeTicksAndLongDateInRowKey()
public void IncludeMicrosInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${micros}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -124,7 +183,7 @@ public void IncludeMicrosInRowKey()
public void IncludeMachineInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${machine}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -142,7 +201,7 @@ public void IncludeMachineInRowKey()
public void IncludeDescendingTicksInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${descticks}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -160,7 +219,7 @@ public void IncludeDescendingTicksInRowKey()
public void IncludeLevelInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${level}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand All @@ -178,7 +237,7 @@ public void IncludeLevelInRowKey()
public void IncludeLevelUppercaseInRowKey()
{
// configure keys
var target = (AzureTableStorageTarget)LogManager.Configuration.FindTargetByName(TargetTableName);
var target = (AzureTableStorageTarget) LogManager.Configuration.FindTargetByName(TargetTableName);
target.RowKey = "${level:uppercase=true}__${guid}";
LogManager.ReconfigExistingLoggers();

Expand Down Expand Up @@ -215,6 +274,7 @@ public void Dispose()
{
_cloudTable.DeleteIfExists();
}

#endregion Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<Reference Include="Newtonsoft.Json">
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog">
<HintPath>..\packages\NLog.3.2.0.0\lib\net45\NLog.dll</HintPath>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
2 changes: 1 addition & 1 deletion NLog.Extensions.AzureTableStorage.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.1.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="NLog" version="3.2.0.0" targetFramework="net45" />
<package id="NLog" version="4.4.12" targetFramework="net45" />
<package id="NLog.Config" version="3.2.0.0" targetFramework="net45" />
<package id="NLog.Schema" version="3.2.0.0" targetFramework="net45" />
<package id="System.Spatial" version="5.6.4" targetFramework="net45" />
Expand Down
23 changes: 21 additions & 2 deletions NLog.Extensions.AzureTableStorage/LogEntity.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections;
using System.Globalization;
using System.Net.NetworkInformation;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.WindowsAzure.Storage.Table;

namespace NLog.Extensions.AzureTableStorage
Expand All @@ -13,14 +13,18 @@ namespace NLog.Extensions.AzureTableStorage
public class LogEntity : TableEntity
{
#region Constants

#endregion Constants

#region Fields

private readonly object _syncRoot = new object();
private static readonly CompareInfo InvariantCompareInfo = CultureInfo.InvariantCulture.CompareInfo;

#endregion Fields

#region Properties

public string LogTimeStamp { get; set; }

public string Level { get; set; }
Expand All @@ -40,9 +44,11 @@ public class LogEntity : TableEntity
public string ExceptionData { get; set; }

public string MachineName { get; set; }

#endregion Properties

#region Constructors

public LogEntity()
{
}
Expand Down Expand Up @@ -81,12 +87,14 @@ public LogEntity(string partitionKey, string rowKey, LogEventInfo logEvent, stri
RowKey = TransformKey(rowKey, logEvent);
}
}

#endregion Constructors

#region Methods

private string TransformKey(string key, LogEventInfo logEvent)
{
var capacity = key.Length * 3; // Guesstimate of a reasonable maximum length after transform
var capacity = key.Length * 3; // Guesstimate of a reasonable maximum length after transform
var builder = new StringBuilder(key, capacity);

var date = logEvent.TimeStamp.ToUniversalTime();
Expand Down Expand Up @@ -117,6 +125,16 @@ private string TransformKey(string key, LogEventInfo logEvent)
if (InvariantCompareInfo.IndexOf(key, "${machine}", CompareOptions.Ordinal) >= 0)
builder.Replace("${machine}", MachineName);

var gdcRegEx = new Regex(@"\$\{(gdc:)(\w{1,})\}", RegexOptions.Compiled);
foreach (Match match in gdcRegEx.Matches(key))
{
var gdcItem = GlobalDiagnosticsContext.Get(match.Groups[2].Value);
if (!string.IsNullOrWhiteSpace(gdcItem))
{
builder.Replace(match.Groups[0].Value, gdcItem);
}
}

var result = builder.ToString();
return result;
}
Expand All @@ -130,6 +148,7 @@ private static string GetExceptionDataAsString(Exception exception)
}
return data.ToString();
}

#endregion Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=3.2.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\NLog.3.2.0.0\lib\net45\NLog.dll</HintPath>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
Expand Down
7 changes: 5 additions & 2 deletions NLog.Extensions.AzureTableStorage/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.4.0")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyVersion("1.1.5.0")]
[assembly: AssemblyFileVersion("1.1.5.0")]
[assembly: NeutralResourcesLanguage("en")]

// actually used for nuget
[assembly: AssemblyInformationalVersion("1.1.5.0")]
2 changes: 1 addition & 1 deletion NLog.Extensions.AzureTableStorage/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<package id="Microsoft.Data.Services.Client" version="5.6.4" targetFramework="net45" />
<package id="Microsoft.WindowsAzure.ConfigurationManager" version="3.1.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net45" />
<package id="NLog" version="3.2.0.0" targetFramework="net45" />
<package id="NLog" version="4.4.12" targetFramework="net45" />
<package id="System.Spatial" version="5.6.4" targetFramework="net45" />
<package id="WindowsAzure.Storage" version="4.3.0" targetFramework="net45" />
</packages>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ This package allows you to use the following macros to format your partition and
- **${level}** will be replaced by the log level. Also supports ${level:uppercase=true}.
- **${machine}** will be replaced by the value of Environment.Machine.
- **${descticks}** will be replaced by the number of ticks *remaining* till <a href="https://msdn.microsoft.com/en-us/library/system.datetime.maxvalue(v=vs.110).aspx">DateTime.MaxValue</a>.
- **${gdc:[keyName]} will be replaced by value from GlobalDiagnosticsContext by keyName

Configuring the Target Dynamically
==================================
Expand Down