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
Expand Up @@ -2,12 +2,12 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using O2NextGen.CertificateManagement.Api.Models;
using O2NextGen.CertificateManagement.Api.Setup;
using O2NextGen.CertificateManagement.Business.Services;
using O2NextGen.CertificateManagement.Web.Mappings;
using O2NextGen.CertificateManagement.Web.Models;
using O2NextGen.CertificateManagement.Web.Setup;
using O2NextGen.CertificateManagement.Api.Mappings;

namespace O2NextGen.CertificateManagement.Web.Controllers
namespace O2NextGen.CertificateManagement.Api.Controllers
{
[Route("certificates")]
public class CertificatesController : ControllerBase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace O2NextGen.CertificateManagement.Web.Controllers
namespace O2NextGen.CertificateManagement.Api.Controllers
{

[AllowAnonymous]
Expand All @@ -13,15 +14,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 @@ -30,6 +33,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
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.EntityFrameworkCore;

namespace O2NextGen.CertificateManagement.Web.Filters
namespace O2NextGen.CertificateManagement.Api.Filters
{
public class ApiExceptionFilter: IExceptionFilter

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Http;

namespace O2NextGen.CertificateManagement.Web.Helpers
namespace O2NextGen.CertificateManagement.Api.Helpers
{
public static class ExceptionExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.CertificateManagement.Api.Filters;
using O2NextGen.CertificateManagement.Business.Services;
using O2NextGen.CertificateManagement.Data;
using O2NextGen.CertificateManagement.Impl.Services;
using O2NextGen.CertificateManagement.Web.Filters;

namespace O2NextGen.CertificateManagement.Web.IoC
namespace O2NextGen.CertificateManagement.Api.IoC
{
public static class ServiceCollectionExtensions
{
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.CertificateManagement.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";
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using O2NextGen.CertificateManagement.Api.Models;
using O2NextGen.CertificateManagement.Business.Models;
using O2NextGen.CertificateManagement.Web.Models;

namespace O2NextGen.CertificateManagement.Web.Mappings
namespace O2NextGen.CertificateManagement.Api.Mappings
{
public static class CertificateMappings
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace O2NextGen.CertificateManagement.Web.Models
namespace O2NextGen.CertificateManagement.Api.Models
{
public class CertificateViewModel
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>O2NextGen.CertificateManagement.Web</RootNamespace>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -39,4 +38,20 @@
<_ContentIncludedByDefault Remove="Views\Certificates\Index.cshtml" />
<_ContentIncludedByDefault Remove="Views\_ViewImports.cshtml" />
</ItemGroup>

<ItemGroup>
<Compile Remove="Logs\**" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Remove="Logs\**" />
</ItemGroup>

<ItemGroup>
<None Remove="Logs\**" />
</ItemGroup>

<ItemGroup>
<Content Remove="Logs\**" />
</ItemGroup>
</Project>
16 changes: 5 additions & 11 deletions src/Services/c-gen/O2NextGen.CertificateManagement.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Hosting;
using Serilog;

namespace O2NextGen.CertificateManagement.Web
namespace O2NextGen.CertificateManagement.Api
{
public class Program
{
Expand All @@ -18,15 +18,6 @@ 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();

var host = CreateWebHostBuilder(args).Build();
Log.Information($"############### {AppName} ##############");
Log.Information("################# Starting Application #################");
Expand All @@ -48,7 +39,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>(); // <- Add this line
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace O2NextGen.CertificateManagement.Web.Setup
namespace O2NextGen.CertificateManagement.Api.Setup
{
public class UrlsConfig
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using O2NextGen.CertificateManagement.Web.Helpers;
using O2NextGen.CertificateManagement.Web.IoC;
using O2NextGen.CertificateManagement.Web.Setup;
using O2NextGen.CertificateManagement.Api.Setup;
using O2NextGen.CertificateManagement.Api.Helpers;
using O2NextGen.CertificateManagement.Api.IoC;

[assembly: ApiController]
namespace O2NextGen.CertificateManagement.Web
namespace O2NextGen.CertificateManagement.Api
{
public class Startup
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
"Auth": "http://localhost:10001"
},
"AllowedHosts": "*",
"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 All @@ -12,4 +29,5 @@
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using O2NextGen.CertificateManagement.Web;
using O2NextGen.CertificateManagement.Api;

namespace IntegrationTests.O2NextGen.CertificateManagement.Api
{
Expand Down