-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for structured logging properties (IncludeEventPropertie…
- Loading branch information
1 parent
848ff44
commit 45524ff
Showing
8 changed files
with
188 additions
and
139 deletions.
There are no files selected for viewing
62 changes: 27 additions & 35 deletions
62
examples/netFx/NLogGraylogHttp.Example.NetFx/NLogGraylogHttp.Example.NetFx.csproj
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 |
---|---|---|
@@ -1,36 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>net471</TargetFramework> | ||
<Configurations>Debug;Release</Configurations> | ||
<OutputType>Exe</OutputType> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'"> | ||
<DocumentationFile>bin\Release\net471\NLogGraylogHttp.Example.NetFx.xml</DocumentationFile> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> | ||
<DocumentationFile>bin\Debug\net471\NLogGraylogHttp.Example.NetFx.xml</DocumentationFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\NLog.Targets.GraylogHttp\NLog.Targets.GraylogHttp.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.0" /> | ||
<PackageReference Include="NLog" Version="4.5.4" /> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="System.Configuration" /> | ||
<Reference Include="System.IO.Compression" /> | ||
<Reference Include="System.Net.Http"> | ||
<Private>True</Private> | ||
</Reference> | ||
<Reference Include="System.Net.Http.WebRequest" /> | ||
<Reference Include="System.ServiceModel" /> | ||
<Reference Include="System.Transactions" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="App.config" /> | ||
</ItemGroup> | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net45</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\src\NLog.Targets.GraylogHttp\NLog.Targets.GraylogHttp.csproj" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.0" /> | ||
<PackageReference Include="NLog" Version="4.5.4" /> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="System.Configuration" /> | ||
<Reference Include="System.IO.Compression" /> | ||
<Reference Include="System.Net.Http" /> | ||
<Reference Include="System.Net.Http.WebRequest" /> | ||
<Reference Include="System.ServiceModel" /> | ||
<Reference Include="System.Transactions" /> | ||
<Reference Include="System.Data.DataSetExtensions" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<ProjectConfigFileName>App.config</ProjectConfigFileName> | ||
</PropertyGroup> | ||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace NLog.Targets.GraylogHttp | ||
{ | ||
internal class GraylogMessageBuilder | ||
{ | ||
private readonly JsonObject _graylogMessage = new JsonObject(); | ||
private readonly Func<int> _timestampGenerator; | ||
|
||
internal GraylogMessageBuilder(Func<int> timestampGenerator = null) | ||
{ | ||
if (timestampGenerator == null) | ||
timestampGenerator = () => (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds; | ||
|
||
_timestampGenerator = timestampGenerator; | ||
} | ||
private static readonly DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); | ||
|
||
public GraylogMessageBuilder WithLevel(LogLevel level) | ||
{ | ||
GelfLevel graylogLevel; | ||
object graylogLevel; | ||
|
||
if (level == LogLevel.Debug) | ||
graylogLevel = GelfLevel.Debug; | ||
else if (level == LogLevel.Fatal) | ||
graylogLevel = GelfLevel.Critical; | ||
if (level == LogLevel.Trace) | ||
graylogLevel = GelfLevel_Debug; | ||
else if (level == LogLevel.Debug) | ||
graylogLevel = GelfLevel_Debug; | ||
else if (level == LogLevel.Info) | ||
graylogLevel = GelfLevel.Informational; | ||
else if (level == LogLevel.Trace) | ||
graylogLevel = GelfLevel.Informational; | ||
graylogLevel = GelfLevel_Informational; | ||
else if (level == LogLevel.Warn) | ||
graylogLevel = GelfLevel_Warning; | ||
else if (level == LogLevel.Fatal) | ||
graylogLevel = GelfLevel_Critical; | ||
else | ||
graylogLevel = level == LogLevel.Warn ? GelfLevel.Warning : GelfLevel.Error; | ||
graylogLevel = GelfLevel_Error; | ||
|
||
return WithProperty("level", (int)graylogLevel); | ||
return WithProperty("level", graylogLevel); | ||
} | ||
|
||
public GraylogMessageBuilder WithProperty(string propertyName, object value) | ||
{ | ||
// Trunate due to https://github.com/Graylog2/graylog2-server/issues/873 | ||
// 32766b max, C# strings 2b per char | ||
_graylogMessage[propertyName] = value.ToString().Truncate(16383); | ||
return this; | ||
{ | ||
if (value is IConvertible primitive && primitive.GetTypeCode() != TypeCode.String) | ||
{ | ||
value = primitive; | ||
} | ||
else | ||
{ | ||
// Trunate due to https://github.com/Graylog2/graylog2-server/issues/873 | ||
// 32766b max, C# strings 2b per char | ||
value = value?.ToString()?.Truncate(16383); | ||
} | ||
|
||
_graylogMessage[propertyName] = value; | ||
return this; | ||
} | ||
|
||
public GraylogMessageBuilder WithCustomProperty(string propertyName, object value) | ||
{ | ||
return WithProperty($"_{propertyName}", value); | ||
} | ||
if (!propertyName.StartsWith("_", StringComparison.Ordinal)) | ||
propertyName = string.Concat("_", propertyName); | ||
|
||
public GraylogMessageBuilder WithCustomPropertyRange(Dictionary<string, string> properties) | ||
{ | ||
return properties.Aggregate(this, (builder, pair) => builder.WithCustomProperty(pair.Key, pair.Value)); | ||
return WithProperty(propertyName, value); | ||
} | ||
|
||
public string Render() | ||
public string Render(DateTime timestamp) | ||
{ | ||
WithProperty("timestamp", _timestampGenerator()); | ||
WithProperty("timestamp", (long)(timestamp.ToUniversalTime() - _epochTime).TotalSeconds); | ||
WithProperty("version", "1.1"); | ||
|
||
return _graylogMessage.ToString(); | ||
} | ||
|
||
public enum GelfLevel | ||
{ | ||
Emergency = 0, | ||
Alert = 1, | ||
Critical = 2, | ||
Error = 3, | ||
Warning = 4, | ||
Notice = 5, | ||
Informational = 6, | ||
Debug = 7 | ||
} | ||
//private static readonly object GelfLevel_Emergency = 0; | ||
//private static readonly object GelfLevel_Alert = 1; | ||
private static readonly object GelfLevel_Critical = 2; | ||
private static readonly object GelfLevel_Error = 3; | ||
private static readonly object GelfLevel_Warning = 4; | ||
//private static readonly object GelfLevel_Notice = 5; | ||
private static readonly object GelfLevel_Informational = 6; | ||
private static readonly object GelfLevel_Debug = 7; | ||
} | ||
} |
Oops, something went wrong.