Skip to content

Commit

Permalink
vnext (#35)
Browse files Browse the repository at this point in the history
* logging fixes
  • Loading branch information
pm7y committed Apr 1, 2021
1 parent 9dbdde5 commit 5a14e6d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 37 deletions.
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ services:
# add an Azure Function subscriber running on localhost (host.docker.internal)
- AEGS_Topics__0__subscribers__1__name=AzureFunctionSubscription
- AEGS_Topics__0__subscribers__1__endpoint=http://host.docker.internal:7071/runtime/webhooks/EventGrid?functionName=ExampleFunction
- AEGS_Topics__0__subscribers__1__disableValidation=true
- AEGS_Topics__0__subscribers__1__disableValidation=false
# logging configuration
- AEGS_Serilog__MinimumLevel__Default=Verbose

# you could also define topics/subscribers via via a configfile
# - ConfigFile=/aegs/appsettings.docker.json
Expand Down
1 change: 1 addition & 0 deletions docker-run.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ if ($(docker ps --all --filter="name=$containerName") -like "*$containerName*")
-e AEGS_Topics__0__subscribers__1__name=AzureFunctionSubscription `
-e AEGS_Topics__0__subscribers__1__endpoint=http://host.docker.internal:7071/runtime/webhooks/EventGrid?functionName=ExampleFunction `
-e AEGS_Topics__0__subscribers__1__disableValidation=true `
-e AEGS_Serilog__MinimumLevel__Default=Verbose `
$imageName
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ public static async Task<string> RequestBody(this HttpContext context)
{
var reader = new StreamReader(context.Request.Body);
reader.BaseStream.Seek(0, SeekOrigin.Begin);

var responseString = await reader.ReadToEndAsync();
reader.BaseStream.Seek(0, SeekOrigin.Begin);

return responseString;
return await reader.ReadToEndAsync();
}

public static async Task WriteErrorResponse(this HttpContext context, HttpStatusCode statusCode, string errorMessage, string code)
Expand Down
34 changes: 9 additions & 25 deletions src/AzureEventGridSimulator/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using AzureEventGridSimulator.Infrastructure.Extensions;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
Expand Down Expand Up @@ -71,7 +71,14 @@ private static IConfigurationRoot BuildConfiguration(string[] args)
.AddJsonFile("appsettings.json", true, false)
.AddJsonFile($"appsettings.{environmentName}.json", true, false)
.AddCustomSimulatorConfigFileIfSpecified(environmentAndCommandLineConfiguration)
.AddEnvironmentVariablesAndCommandLine(args);
.AddEnvironmentVariablesAndCommandLine(args)
.AddInMemoryCollection(
new Dictionary<string, string>
{
["AEGS_Serilog__Using__0"] = "Serilog.Sinks.Console",
["AEGS_Serilog__Using__1"] = "Serilog.Sinks.File",
["AEGS_Serilog__Using__2"] = "Serilog.Sinks.Seq"
});

return builder.Build();
}
Expand All @@ -88,8 +95,6 @@ private static IWebHostBuilder ConfigureWebHost(string[] args, IConfiguration co
.ConfigureLogging(builder => { builder.ClearProviders(); })
.UseSerilog((context, loggerConfiguration) =>
{
ShowSerilogUsingWarningIfNecessary(context.Configuration);
var hasAtLeastOneLogSinkBeenConfigured = context.Configuration.GetSection("Serilog:WriteTo").GetChildren().ToArray().Any();
loggerConfiguration
Expand Down Expand Up @@ -121,26 +126,5 @@ private static IWebHostBuilder ConfigureWebHost(string[] args, IConfiguration co
}
});
}

private static void ShowSerilogUsingWarningIfNecessary(IConfiguration config)
{
var usingNeedsToBeConfigured = config.GetSection("Serilog").Exists() &&
!config.GetSection("Serilog:Using").Exists();

if (usingNeedsToBeConfigured)
{
// Warn the user about the necessity for the serilog using section with .net 5.0.
// https://github.com/serilog/serilog-settings-configuration#net-50-single-file-applications
var msg = new StringBuilder();

msg.AppendLine(@"Serilog with .net 5.0 now requires a 'Using' section.");
msg.AppendLine("Please add the following to the 'Serilog' config section and restart: -" + Environment.NewLine);
msg.AppendLine(@"""Using"": [""Serilog.Sinks.Console"", ""Serilog.Sinks.File"", ""Serilog.Sinks.Seq""]");

Log.Fatal(msg.ToString());

Environment.Exit(-1);
}
}
}
}
5 changes: 4 additions & 1 deletion src/AzureEventGridSimulator/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ public void Configure(IApplicationBuilder app,

app.UseSerilogRequestLogging(options => { options.GetLevel = (httpContext, elapsed, ex) => LogEventLevel.Debug; });
app.UseEventGridMiddleware();
app.UseMvc();
app.UseRouting();
app.UseEndpoints(e => {
e.MapControllers();
});
}

private static async Task OnApplicationStarted(IApplicationBuilder app, IHostApplicationLifetime lifetime, ILogger logger)
Expand Down
5 changes: 0 additions & 5 deletions src/AzureEventGridSimulator/example.appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@
"System": "Warning"
}
},
"Using": [
"Serilog.Sinks.Console",
"Serilog.Sinks.File",
"Serilog.Sinks.Seq"
],
"WriteTo": [
{ "Name": "Console" },
{
Expand Down

0 comments on commit 5a14e6d

Please sign in to comment.