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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using O2NextGen.ESender.Api.Models;
Expand All @@ -7,7 +7,7 @@ namespace O2NextGen.ESender.Api.Controllers
{
[Route("emailsender")]
public class EmailSenderController : Controller
{
{
private static long _currentCertificateId = 1;

private static List<MailViewModel> _mailLetters = new List<MailViewModel>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace O2NextGen.ESender.Api.Controllers
{
Expand All @@ -12,15 +13,17 @@ public class VersionController : ControllerBase
#region Fields

private readonly IHostingEnvironment _environment;
private readonly ILogger<VersionController> _logger;

#endregion


#region Ctors

public VersionController(IHostingEnvironment environment)
public VersionController(IHostingEnvironment environment, ILogger<VersionController> logger)
{
_environment = environment;
_logger = logger;
}

#endregion
Expand All @@ -29,6 +32,7 @@ public VersionController(IHostingEnvironment environment)
public object Index()
{
var exVersion = Assembly.GetExecutingAssembly().GetName().Version;
_logger.LogInformation($"get version - {exVersion}");
return new
{
Environment = _environment.EnvironmentName,
Expand Down
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.ESender.Api.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";
}
}
}
}
21 changes: 12 additions & 9 deletions src/Services/e-sender/O2NextGen.ESender.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ public static async Task<int> Main(string[] args)
{
try
{
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
#if DEBUG
.WriteTo.File("Logs/system_logs.txt")
#endif
.WriteTo.Console()

.CreateLogger();
// Log.Logger = new LoggerConfiguration()
// .Enrich.FromLogContext()
// #if DEBUG
// .WriteTo.File("Logs/system_logs.txt")
// #endif
// .WriteTo.Console()
//
// .CreateLogger();

var host = CreateWebHostBuilder(args).Build();
Log.Information($"############### {AppName} ##############");
Expand All @@ -46,7 +46,10 @@ public static async Task<int> Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseSerilog()
.UseSerilog((context, configuration) =>
{
configuration.ReadFrom.Configuration(context.Configuration);
})
.UseStartup<Startup>();
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Services/e-sender/O2NextGen.ESender.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseMvc();
}
Expand Down
17 changes: 17 additions & 0 deletions src/Services/e-sender/O2NextGen.ESender.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
{
"Serilog": {
"Enrich": [
"FromLogContext"
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "Logs/log.txt",
"formatter": "O2NextGen.ESender.Api.Logging.ElasticJsonFormatter, O2NextGen.ESender.Api"
}
},
{
"Name": "Console"
}
]
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
Expand Down