Skip to content

Commit

Permalink
Update samples and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwcole committed Nov 18, 2018
1 parent d33b573 commit e1753e4
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 65 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@ var webHost = WebHost
.UseStartup<Startup>()
.ConfigureLogging((context, builder) =>
{
// Read Logging settings from appsettings.json and add providers.
builder.AddConfiguration(context.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
.AddGelf();

// Optionally configure GELF logger further.
builder.Services.PostConfigure<GelfLoggerOptions>(options =>
options.AdditionalFields["machine_name"] = Environment.MachineName);
.AddGelf(options =>
{
// Optional customisation applied on top of settings in Logging:GELF configuration section.
options.LogSource = context.HostingEnvironment.ApplicationName;
options.AdditionalFields["machine_name"] = Environment.MachineName;
options.AdditionalFields["app_version"] = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
});
})
.Build();
```

You can then configure the "GELF" provider in `appsettings.json` in the same way as other providers.
Logger options are taken from the "GELF" provider section in `appsettings.json` in the same way as other providers. These can be customised further in code as in the above example.

```json
```json5
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Error"
},
Expand All @@ -45,11 +46,14 @@ You can then configure the "GELF" provider in `appsettings.json` in the same way
},
"GELF": {
"Host": "localhost",
"Port": 12201,
"LogSource": "application-name",
"IncludeScopes": true,
"Port": 12201, // Not required if using default 12201.
"LogSource": "my-app-name", // Required if not set in code.
"AdditionalFields": { // Optional fields added to all logs.
"project_name": "my-project-name"
},
"LogLevel": {
"Default": "Information"
"Default": "Information",
"Some.Namespace": "Debug"
}
}
}
Expand Down Expand Up @@ -102,7 +106,7 @@ var options = new GelfLoggerOptions

#### Scoped Fields

Log scopes can also be used to attach fields to a group of related logs. Create a log scope with a [`ValueTuple<string, string>`](https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/), `ValueTuple<string, int/byte/double>` (or any other numeric value) or `Dictionary<string, object>` to do so. _Note that any other types passed to `BeginScope()` will be ignored, including `Dictionary<string, string>` and `ValueTuple<string, object>`._
Log scopes can also be used to attach fields to a group of related logs. Create a log scope with a [`ValueTuple<string, string>`](https://blogs.msdn.microsoft.com/dotnet/2017/03/09/new-features-in-c-7-0/), `ValueTuple<string, int/byte/double>` (or any other numeric value) or `Dictionary<string, object>` to do so. _Note that any other types passed to `BeginScope()` will be ignored, including `Dictionary<string, string>`._

```csharp
using (_logger.BeginScope(("correlation_id", correlationId)))
Expand Down Expand Up @@ -145,4 +149,4 @@ This repository contains a Docker Compose file that can be used for creating loc

## Contributing

Pull requests welcome! In order to run tests, first run `docker-compose up` to create the Graylog stack. Existing tests log messages and use the Graylog API to assert that they have been sent correctly. A UDP input will be created as part of the test setup (if not already present), so there is no need to create one manually. Build and tests are run on CI in Docker, meaning it is possible to run the build locally in identical conditions using `docker-compose -f docker-compose.ci.build.yml -f docker-compose.yml up --abort-on-container-exit`.
Pull requests welcome! In order to run tests, first run `docker-compose up` to create the Graylog stack. Existing tests log messages and use the Graylog API to assert that they have been sent correctly. A UDP input will be created as part of the test setup (if not already present), so there is no need to create one manually. Build and tests are run on CI in Docker, meaning it is possible to run the build locally under identical conditions using `docker-compose -f docker-compose.ci.build.yml -f docker-compose.yml up --abort-on-container-exit`.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IEnumerable<string> Get()
{
var result = new[] {"bar", "baz"};

_logger.LogInformation("Returning {value1} and {value2} from controller", result[0], result[1]);
_logger.LogTrace("Returning {value1} and {value2} from controller", result[0], result[1]);

return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
<PackageReference Include="Gelf.Extensions.Logging" Version="1.5.0-pre1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
Expand Down
24 changes: 12 additions & 12 deletions samples/Gelf.Extensions.Logging.Samples.AspNetCore2/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Gelf.Extensions.Logging.Samples.AspNetCore2
{
public class Program
{
public static void Main(string[] args)
public static Task Main(string[] args)
{
BuildWebHost(args).Run();
return BuildWebHost(args).RunAsync();
}

public static IWebHost BuildWebHost(string[] args)
Expand All @@ -19,18 +20,17 @@ public static IWebHost BuildWebHost(string[] args)
.UseStartup<Startup>()
.ConfigureLogging((context, builder) =>
{
// Read GelfLoggerOptions from appsettings.json.
builder.Services.Configure<GelfLoggerOptions>(context.Configuration.GetSection("Graylog"));

// Optionally configure GelfLoggerOptions further.
builder.Services.PostConfigure<GelfLoggerOptions>(options =>
options.AdditionalFields["machine_name"] = Environment.MachineName);

// Read Logging settings from appsettings.json and add providers.
builder.AddConfiguration(context.Configuration.GetSection("Logging"))
.AddConsole()
.AddDebug()
.AddGelf();
.AddGelf(options =>
{
// Optional config combined with Logging:GELF configuration section.
options.LogSource = context.HostingEnvironment.ApplicationName;
options.AdditionalFields["machine_name"] = Environment.MachineName;
options.AdditionalFields["app_version"] = Assembly.GetEntryAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
});
})
.Build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
"Default": "Warning"
},
"Console": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Microsoft.AspNetCore.Mvc.Razor": "Error"
}
},
"GELF": {
"IncludeScopes": true,
"Host": "localhost",
"AdditionalFields": {
"project_name": "my-project"
},
"LogLevel": {
"Default": "Debug"
"Default": "Information",
"Gelf.Extensions.Logging.Samples.AspNetCore2.Controllers": "Debug"
}
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Gelf.Extensions.Logging" Version="1.5.0-pre1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-1",
"LogLevel": "Trace"
"LogLevel": "Trace",
"AdditionalFields": {
"project_name": "my-project"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand All @@ -14,11 +14,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.4.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.0" />
<PackageReference Include="Gelf.Extensions.Logging" Version="1.5.0-pre1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions samples/Gelf.Extensions.Logging.Samples.NetCore2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public static void Main()
.Build();

var serviceCollection = new ServiceCollection()
.Configure<GelfLoggerOptions>(configuration.GetSection("Graylog"))
.AddLogging(loggingBuilder => loggingBuilder
.AddConfiguration(configuration.GetSection("Logging"))
.AddConsole()
.AddGelf());
.AddGelf(options => options.AdditionalFields["machine_name"] = Environment.MachineName));

// The LoggerFactory must be disposed before the program exits to ensure all queued messages are sent.
using (var serviceProvider = serviceCollection.BuildServiceProvider())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
}
},
"GELF": {
"Host": "localhost",
"LogSource": "console-app-2",
"AdditionalFields": {
"project_name": "my-project"
},
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Trace"
}
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-2"
}
}
5 changes: 2 additions & 3 deletions src/Gelf.Extensions.Logging/LoggingBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public static class LoggingBuilderExtensions
{
/// <summary>
/// Registers a <see cref="GelfLoggerProvider"/> with the service collection.
/// <see cref="GelfLoggerOptions"/> must be configured with service collection when using this method.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
Expand All @@ -27,8 +26,8 @@ public static ILoggingBuilder AddGelf(this ILoggingBuilder builder)
}

/// <summary>
/// Registers a <see cref="GelfLoggerProvider"/> and <see cref="GelfLoggerOptions"/>
/// with the service collection.
/// Registers a <see cref="GelfLoggerProvider"/> with the service collection allowing logger options
/// to be customised.
/// </summary>
/// <param name="builder"></param>
/// <param name="configure"></param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ public void Reads_GELF_logger_options_from_logging_configuration_section_by_defa
{
["Logging:GELF:IncludeScopes"] = "false",
["Logging:GELF:Protocol"] = "HTTP",
["Logging:GELF:Host"] = "graylog-host-1",
["Graylog:IncludeScopes"] = "true",
["Graylog:Protocol"] = "HTTPS",
["Graylog:Host"] = "graylog-host2"
["Logging:GELF:Host"] = "graylog-host-1"
}
}).Build();

var serviceCollection = new ServiceCollection()
.PostConfigure<GelfLoggerOptions>(o => o.LogSource = "post-configured-log-source")
.AddLogging(loggingBuilder => loggingBuilder
.AddConfiguration(configuration.GetSection("Logging"))
.AddGelf());
.AddGelf(o => o.LogSource = "post-configured-log-source"));

using (var provider = serviceCollection.BuildServiceProvider())
{
Expand Down Expand Up @@ -64,7 +60,7 @@ public void Reads_GELF_logger_options_from_custom_configuration_section()
.PostConfigure<GelfLoggerOptions>(o => o.LogSource = "post-configured-log-source")
.AddLogging(loggingBuilder => loggingBuilder
.AddConfiguration(configuration.GetSection("Logging"))
.AddGelf());
.AddGelf(o => o.LogSource = "post-configured-log-source"));

using (var provider = serviceCollection.BuildServiceProvider())
{
Expand Down

0 comments on commit e1753e4

Please sign in to comment.