Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/Services/auth/O2NextGen.Auth.Web/Logging/ElasticJsonFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Formatting.Json;

namespace O2NextGen.Auth.Web.Logging
{
public class ElasticJsonFormatter : ITextFormatter
{
private const string Prefix = "[ELASTIC]";

private readonly NamingStrategy namingStrategy;
private readonly JsonValueFormatter formatter;
private readonly string type;

public ElasticJsonFormatter(NamingStrategy namingStrategy, JsonValueFormatter formatter, string type)
{
this.namingStrategy = namingStrategy;
this.formatter = formatter;
this.type = type;
}

public ElasticJsonFormatter()
: this(new CamelCaseNamingStrategy(), new JsonValueFormatter("$type"), "commspoint")
{
}

public void Format(LogEvent logEvent, TextWriter output)
{
output.Write(Prefix);
FormatBody(logEvent, output);
output.WriteLine();
}

private void FormatBody(LogEvent logEvent, TextWriter output)
{
// add json body
var writer = new JsonTextWriter(output);
writer.WriteStartObject();

// write level
writer.WritePropertyName("level");
writer.WriteValue(FormatLogLevel(logEvent.Level));

// write type
writer.WritePropertyName("type");
writer.WriteValue(type);

// write timestamp
writer.WritePropertyName("timestamp");
writer.WriteValue(logEvent.Timestamp.UtcDateTime);

// write message
writer.WritePropertyName("message");
writer.WriteValue(logEvent.RenderMessage());

// write exception if exists
if (logEvent.Exception != null)
{
writer.WritePropertyName("exception");
writer.WriteValue(logEvent.Exception.ToString());
}

// write properties
writer.WritePropertyName("properties");
writer.WriteStartObject();
foreach (var property in logEvent.Properties)
{
var propertyName = namingStrategy.GetPropertyName(property.Key, false);
writer.WritePropertyName(propertyName);

using (var stringWriter = new StringWriter())
{
formatter.Format(property.Value, stringWriter);
writer.WriteRawValue(stringWriter.ToString());
}
}

writer.WriteEndObject();
writer.Flush();
}

private static string FormatLogLevel(LogEventLevel level)
{
switch (level)
{
case LogEventLevel.Verbose:
return "TRACE";
case LogEventLevel.Debug:
return "DEBUG";
case LogEventLevel.Warning:
return "WARNING";
case LogEventLevel.Error:
case LogEventLevel.Fatal:
return "ERROR";
default:
return "INFO";
}
}
}
}
10 changes: 10 additions & 0 deletions src/Services/auth/O2NextGen.Auth.Web/O2NextGen.Auth.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
<PackageReference Include="Serilog.AspNetCore" Version="2.1.1">
</PackageReference>
<PackageReference Include="Serilog.Settings.Configuration" Version="2.2.0">
</PackageReference>
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0">
</PackageReference>
<PackageReference Include="Serilog.Sinks.Console" Version="2.1.0">
</PackageReference>
<PackageReference Include="Serilog.Sinks.File" Version="2.2.0">
</PackageReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Resources\Pages\Account\Register.en.resx">
Expand Down
35 changes: 32 additions & 3 deletions src/Services/auth/O2NextGen.Auth.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
using Microsoft.AspNetCore;
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Serilog;

namespace O2NextGen.Auth.Web
{
public class Program
{
public static void Main(string[] args)
public static readonly string Namespace = typeof(Program).Namespace;

public static readonly string AppName =
Namespace.Substring(Namespace.LastIndexOf('.', Namespace.LastIndexOf('.') - 1) + 1);

public static async Task<int> Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
try
{
var host = CreateWebHostBuilder(args).Build();
Log.Information($"############### {AppName} ##############");
Log.Information("################# Starting Application #################");
await host.RunAsync();
Log.Information($"============== {AppName} - state is started =====================");
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
Log.CloseAndFlush();
}
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog((context, configuration) =>
{
configuration.ReadFrom.Configuration(context.Configuration);
})
.UseStartup<Startup>();
}
}
22 changes: 21 additions & 1 deletion src/Services/auth/O2NextGen.Auth.Web/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
{
"Serilog": {
"Enrich": [
"FromLogContext"
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log.txt",
"formatter": "O2NextGen.Auth.Web.Logging.ElasticJsonFormatter, O2NextGen.Auth.Web"
}
},
{
"Name": "Console"
}
]
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
Expand Down