Skip to content

Commit

Permalink
Add some logging back into the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfwessels committed Dec 5, 2023
1 parent ea070bc commit 7063816
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 8 deletions.
23 changes: 23 additions & 0 deletions src/CoreDocker.Api/Logging.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using CoreDocker.Core.Framework.Logging;
using Serilog.Events;
using Path = System.IO.Path;

namespace CoreDocker.Api;

public static class Logging
{
public static void AddSerilog(this WebApplicationBuilder builder)
{
Log.Logger = LoggingHelper.SetupOnce(() => new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
.WriteTo.File(Path.Combine(Path.GetTempPath(), "logs", "CoreDocker.Api.log"),
fileSizeLimitBytes: 10 * LoggingHelper.MB,
rollOnFileSizeLimit: true)
.CreateLogger());
builder.Logging.ClearProviders();
builder.Logging.AddSerilog(Log.Logger);
}
}
5 changes: 2 additions & 3 deletions src/CoreDocker.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Runtime.CompilerServices;
using CoreDocker.Api;
using CoreDocker.Api.AppStartup;
using CoreDocker.Api.GraphQl;
using CoreDocker.Api.Security;
Expand All @@ -11,7 +11,7 @@

var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddJsonFilesAndEnvironment();

builder.AddSerilog();

builder.Services.AddMediatR(cfg => cfg
.RegisterServicesFromAssemblyContaining<Program>()
Expand Down Expand Up @@ -62,7 +62,6 @@
app.Run();



namespace CoreDocker.Api
{
public partial class Program
Expand Down
2 changes: 1 addition & 1 deletion src/CoreDocker.Api/Security/OpenIdSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OpenIdSettings(IConfiguration configuration) : base(configuration, "OpenI

public bool UseReferenceTokens => ReadConfigValue("UseReferenceTokens", true);

public bool IsDebugEnabled => ReadConfigValue("IsDebugEnabled", true);
public bool IsDebugEnabled => ReadConfigValue("IsDebugEnabled", false);

public string[] GetOriginList()
{
Expand Down
1 change: 1 addition & 0 deletions src/CoreDocker.Api/Security/SecuritySetupClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public static void AddAuthenticationClient(this IServiceCollection services, Ope
services.AddDistributedMemoryCache();
services.AddAuthorization(AddFromActivities);
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddCookie(SecuritySetupServer.CookieAuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = idSettings.HostUrl;
Expand Down
10 changes: 7 additions & 3 deletions src/CoreDocker.Api/Security/SecuritySetupServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ namespace CoreDocker.Api.Security
{
public static class SecuritySetupServer
{
private static readonly ILogger _log = Log.ForContext(MethodBase.GetCurrentMethod()?.DeclaringType);

public const string CookieAuthenticationScheme = "CoreDockerCookie";
private static readonly ILogger _log = Log.ForContext(MethodBase.GetCurrentMethod()!.DeclaringType!);

public static void AddIdentityService(this IServiceCollection services, IConfiguration configuration)
{
var openIdSettings = new OpenIdSettings(configuration);
_log.Debug($"SecuritySetupServer:UseIdentityService Setting the host url {openIdSettings.HostUrl}");
services.AddIdentityServer()
services.AddIdentityServer(options=>
{
options.IssuerUri = openIdSettings.HostUrl;
options.Authentication.CookieAuthenticationScheme = CookieAuthenticationScheme;
})
.AddSigningCredential(Certificate(openIdSettings.CertPfx, openIdSettings.CertPassword,
openIdSettings.CertStoreThumbprint))
.AddInMemoryIdentityResources(OpenIdConfig.GetIdentityResources())
Expand Down
1 change: 1 addition & 0 deletions src/CoreDocker.Core/Framework/Logging/LoggingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public static ILogger SetupOnce(Func<ILogger> func)

return Log.Logger;
}

}
}
1 change: 0 additions & 1 deletion test/CoreDocker.Api.Tests/IntegrationTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ private void SetEnvironmentVariable(string variable, string value)

public HttpClient CreateClient(string name)
{
Console.Out.WriteLine("name:"+name);
return CreateClient();
}
}
Expand Down

0 comments on commit 7063816

Please sign in to comment.