Skip to content

Commit

Permalink
Add ASP.NET Core example to samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwcole committed May 8, 2018
1 parent 59e8294 commit f919433
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ indent_style = space
trim_trailing_whitespace = true
insert_final_newline = true

[*.json]
indent_size = 2

[*.cs]
indent_size = 4
max_line_length = 120
7 changes: 7 additions & 0 deletions Gelf.Extensions.Logging.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gelf.Extensions.Logging.Sam
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gelf.Extensions.Logging.Samples.NetCore1", "samples\Gelf.Extensions.Logging.Samples.NetCore1\Gelf.Extensions.Logging.Samples.NetCore1.csproj", "{C55B368D-E4C1-42D0-97C2-4AD2F443743B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gelf.Extensions.Logging.Samples.AspNetCore2", "samples\Gelf.Extensions.Logging.Samples.AspNetCore2\Gelf.Extensions.Logging.Samples.AspNetCore2.csproj", "{1499E4E4-EF81-4309-A664-8FEC115B5C82}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -51,6 +53,10 @@ Global
{C55B368D-E4C1-42D0-97C2-4AD2F443743B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C55B368D-E4C1-42D0-97C2-4AD2F443743B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C55B368D-E4C1-42D0-97C2-4AD2F443743B}.Release|Any CPU.Build.0 = Release|Any CPU
{1499E4E4-EF81-4309-A664-8FEC115B5C82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1499E4E4-EF81-4309-A664-8FEC115B5C82}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1499E4E4-EF81-4309-A664-8FEC115B5C82}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1499E4E4-EF81-4309-A664-8FEC115B5C82}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -60,6 +66,7 @@ Global
{4B299C4A-3A24-47B3-83D1-251D5FBDA174} = {7999DEB6-1492-41CF-A94C-63606C616565}
{0A2D1AF3-16F9-4081-A3BE-35331BEF58B5} = {7D9416E1-13A5-4A86-A3F1-2289369D7193}
{C55B368D-E4C1-42D0-97C2-4AD2F443743B} = {7D9416E1-13A5-4A86-A3F1-2289369D7193}
{1499E4E4-EF81-4309-A664-8FEC115B5C82} = {7D9416E1-13A5-4A86-A3F1-2289369D7193}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C6D74A71-1746-4865-AAE2-77625B3E8935}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Usage

The following examples are for ASP.NET Core. The [samples](/samples) directory contains example console apps. For more information on providers and logging in general, see the aspnetcore [logging documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x#how-to-add-providers).
The following examples are for ASP.NET Core. The [samples](/samples) directory contains example console apps with and without ASP.NET Core. For more information on providers and logging in general, see the aspnetcore [logging documentation](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x#how-to-add-providers).

### ASP.NET Core 2.x

Expand Down Expand Up @@ -127,11 +127,11 @@ In the example above, the message will contain an `order_id` and `order_time`.

### Log Filtering

When using .NET Core 1.x, log filtering can be overridden by setting a custom filter with `GelfLoggerOptions.Filter`, overriding the default filter that uses `GelfLoggerOptions.LogLevel`. In .NET Core 2.x, the log filtering API should be used to filter the "GELF" provider (details [here](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x#log-filtering)).
When using .NET Core 2.x, the log filtering API should be used to filter the "GELF" provider (details [here](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging?tabs=aspnetcore2x#log-filtering)). In .NET Core 1.x, log filtering can be overridden by setting a custom filter with `GelfLoggerOptions.Filter`, overriding the default filter that uses `GelfLoggerOptions.LogLevel`.

### Testing

This repository contains a Docker Compose file that can be used for creating local a Graylog stack with a single command using the [Graylog Docker image](https://hub.docker.com/r/graylog2/server/). This can be useful for testing application logs locally. Requires [Docker](https://www.docker.com/get-docker) and Docker Compose.
This repository contains a Docker Compose file that can be used for creating local a Graylog stack with a single command using the [Graylog Docker image](https://hub.docker.com/r/graylog/graylog/). This can be useful for testing application logs locally. Requires [Docker](https://www.docker.com/get-docker) and Docker Compose.

- `docker-compose up`
- Navigate to [http://localhost:9000](http://localhost:9000)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Gelf.Extensions.Logging.Samples.AspNetCore2.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly ILogger<ValuesController> _logger;

public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
_logger.LogDebug("Values controller initialising");
}

[HttpGet]
public IEnumerable<string> Get()
{
using (_logger.BeginScope(("method_name", nameof(Get))))
{
var result = new[] {"foo", "bar"};

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

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

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>

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

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.2.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
</ItemGroup>

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions samples/Gelf.Extensions.Logging.Samples.AspNetCore2/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
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)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args)
{
return WebHost.CreateDefaultBuilder(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();
})
.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50718/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Gelf.Extensions.Logging.Samples.AspNetCore2": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:50719/"
}
}
}
32 changes: 32 additions & 0 deletions samples/Gelf.Extensions.Logging.Samples.AspNetCore2/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Gelf.Extensions.Logging.Samples.AspNetCore2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseMvc();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information"
},
"Console": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Microsoft.AspNetCore.Mvc.Razor": "Error"
}
},
"GELF": {
"IncludeScopes": true,
"LogLevel": {
"Default": "Debug"
}
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.2.1" />
<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" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Gelf.Extensions.Logging\Gelf.Extensions.Logging.csproj" />
</ItemGroup>

</Project>
24 changes: 12 additions & 12 deletions samples/Gelf.Extensions.Logging.Samples.NetCore1/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"Logging": {
"Console": {
"LogLevel": {
"Default": "Debug"
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-1",
"LogLevel": "Trace"
}
"Logging": {
"Console": {
"LogLevel": {
"Default": "Debug"
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-1",
"LogLevel": "Trace"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Gelf.Extensions.Logging" Version="1.2.1" />
<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" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Gelf.Extensions.Logging\Gelf.Extensions.Logging.csproj" />
</ItemGroup>

</Project>
44 changes: 22 additions & 22 deletions samples/Gelf.Extensions.Logging.Samples.NetCore2/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
"Logging": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Default": "Information"
},
"Console": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Default": "Debug"
}
},
"GELF": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Trace"
}
}
"Logging": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug",
"Default": "Information"
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-2"
}
"Console": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning",
"Default": "Debug"
}
},
"GELF": {
"LogLevel": {
"Microsoft.AspNetCore.Mvc.Razor": "Error",
"Default": "Trace"
}
}
},
"Graylog": {
"Host": "localhost",
"Port": 12201,
"LogSource": "console-app-2"
}
}

0 comments on commit f919433

Please sign in to comment.