Belgrade dmeo upgrades to .net core 2.1 and added NLog

This commit is contained in:
Jovan Popovic
2018-10-04 09:52:21 +02:00
parent 649efa4ead
commit 96850338ae
9 changed files with 129 additions and 40 deletions
@@ -10,4 +10,6 @@ Properties/PublishProfiles/*
appsettings.Development.json
appsettings.Production.json
*.ndjson
sql-scripts/bcp.sql.sql
*.tsjson
logs/internal-nlog.txt
sql-scripts/bcp.sql
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProductCatalog.Models;
using System;
using System.Collections.Generic;
@@ -9,9 +10,11 @@ namespace ProductCatalog.Controllers
public class ProductCatalogController : Controller
{
private ProductCatalogContext _context;
public ProductCatalogController (ProductCatalogContext context)
private readonly ILogger<ProductCatalogController> _logger;
public ProductCatalogController (ProductCatalogContext context, ILogger<ProductCatalogController> logger)
{
_logger = logger;
_context = context;
}
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using NLog.Web;
using System.IO;
namespace ProductCatalog
@@ -12,6 +13,7 @@ namespace ProductCatalog
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseNLog()
.UseStartup<Startup>()
.Build();
@@ -1,6 +1,5 @@
using Belgrade.SqlClient;
using Belgrade.SqlClient.SqlDb;
using Belgrade.SqlClient.SqlDb.Rls;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
@@ -9,31 +8,35 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ProductCatalog.Models;
using Serilog;
using Serilog.Sinks;
using Serilog;
using Serilog.Sinks.MSSqlServer;
using System;
using System.Data.SqlClient;
using System.Linq;
using NLog.Extensions.Logging;
using NLog.Web;
namespace ProductCatalog
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
env.ConfigureNLog("nlog.config");
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
#if NETCOREAPP2_0
Log.Logger = new LoggerConfiguration()
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
.CreateLogger();
#endif
#if NET46
// Enable this if you want to log into local folder as newline-delimited JSON
//Log.Logger = new LoggerConfiguration()
// .WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
// .CreateLogger();
var columnOptions = new ColumnOptions();
// Don't include the Properties XML column.
columnOptions.Store.Remove(StandardColumn.Id);
@@ -47,7 +50,6 @@ namespace ProductCatalog
Log.Logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(Configuration["ConnectionStrings:BelgradeDemo"], "Logs", columnOptions: columnOptions, autoCreateSqlTable: false)
.CreateLogger();
#endif
}
public IConfigurationRoot Configuration { get; }
@@ -72,6 +74,7 @@ namespace ProductCatalog
//// Add framework services.
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
services.AddSession();
services.AddMvc();
@@ -84,6 +87,11 @@ namespace ProductCatalog
loggerFactory.AddDebug();
loggerFactory.AddSerilog();
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
NLog.LogManager.ThrowExceptions = true;
NLog.LogManager.ThrowConfigExceptions = true;
app.UseSession();
app.UseStaticFiles();
app.UseMvc(routes =>
@@ -5,9 +5,10 @@
<AssemblyName>belgrade-product-catalog-demo</AssemblyName>
<OutputType>Exe</OutputType>
<PackageId>belgrade-product-catalog-demo</PackageId>
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile />
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
<ApplicationIcon />
<StartupObject />
</PropertyGroup>
<ItemGroup>
@@ -18,35 +19,41 @@
<ItemGroup>
<PackageReference Include="Belgrade.Sql.Client" Version="1.1.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.0.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
<PackageReference Include="Serilog" Version="2.5.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="1.3.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
<PackageReference Include="NLog" Version="4.5.10" />
<PackageReference Include="NLog.Extensions.AzureStorage" Version="1.0.9" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0" />
<PackageReference Include="Serilog" Version="2.7.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.3-dev-00224" />
</ItemGroup>
<ItemGroup>
<None Update="sql-scripts\bcp.sql.sql">
<None Update="nlog.config">
<CopyToOutputDirectory>Preserve Newest</CopyToOutputDirectory>
</None>
<None Update="sql-scripts\bcp.sql">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>bcp.sql.tt</DependentUpon>
<DependentUpon>bcp.tt</DependentUpon>
</None>
<None Update="sql-scripts\bcp.sql.tt">
<None Update="sql-scripts\bcp.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>bcp.sql.sql</LastGenOutput>
<LastGenOutput>bcp.sql</LastGenOutput>
</None>
</ItemGroup>
@@ -0,0 +1,6 @@
12.0
4
1 SQLCHAR 0 30 "\t" 1 EventTime SQL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 20 "\t" 2 Level SQL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 4000 "\t" 3 Message SQL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 80000 "\r\n" 4 EventData SQL_Latin1_General_CP1_CI_AS
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="info"
internalLogFile=".\logs\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
<add assembly="NLog.Extensions.AzureStorage"/>
</extensions>
<!-- the targets to write to -->
<targets>
<target xsi:type="File" name="file" fileName="${currentdir}\logs\log-${shortdate}.tsjson">
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false" quoting="Nothing">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level:upperCase=true}"/>
<column name="message" layout="${message}" />
<column name="data">
<layout xsi:type="JsonLayout">
<attribute name="callsite" layout="${callsite:includeSourcePath=true}" />
<attribute name="stacktrace" layout="${stacktrace:topFrames=10}" />
<attribute name="exception" layout="${exception:format=ToString}"/>
</layout>
</column>
</layout>
</target>
<target xsi:type="AzureBlobStorage"
name="blob"
blobName="log-${shortdate}.tsjson"
container="logs"
connectionString="DefaultEndpointsProtocol=https;AccountName=********;AccountKey=******************************;EndpointSuffix=core.windows.net"
>
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false" quoting="Nothing">
<column name="time" layout="${longdate}" />
<column name="level" layout="${level:upperCase=true}"/>
<column name="message" layout="${message}" />
<column name="data">
<layout xsi:type="JsonLayout">
<attribute name="callsite" layout="${callsite:includeSourcePath=true}" />
<attribute name="stacktrace" layout="${stacktrace:topFrames=10}" />
<attribute name="exception" layout="${exception:format=ToString}"/>
</layout>
</column>
</layout>
</target>
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="file" />
<logger name="*" minlevel="Trace" writeTo="blob" />
</rules>
</nlog>
@@ -3,14 +3,16 @@
SELECT *
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson',
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' ) as logs;
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\ndjson.fmt' ) as logs;
SELECT *
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.tsjson',
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\tsjson.fmt' ) as logs;
DROP TABLE IF EXISTS Logs
DROP TABLE IF EXISTS DataLog
--{"Timestamp":"2017-02-03T08:33:32.0776155+01:00","Level":"Information","MessageTemplate":"{HostingRequestFinished:l}","Properties":{"ElapsedMilliseconds":154.2332,"StatusCode":200,"ContentType":null,"HostingRequestFinished":"Request finished in 154.2332ms 200 ","EventId":{"Id":2},"SourceContext":"Microsoft.AspNetCore.Hosting.Internal.WebHost","RequestId":"0HL2C0RQ64LNN","RequestPath":"/"},"Renderings":{"HostingRequestFinished":[{"Format":"l","Rendering":"Request finished in 154.2332ms 200 "}]}}
CREATE TABLE Logs (
CREATE TABLE LogData (
Data NVARCHAR(MAX),
Timestamp AS CAST(JSON_VALUE(Data, '$.Timestamp') as datetime2),
Level AS CAST(JSON_VALUE(Data, '$.Level') as nvarchar(40)),
@@ -18,6 +20,7 @@ FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjso
StatusCode AS CAST(JSON_VALUE(Data, '$.Properties.StatusCode') AS int)
)
BULK INSERT Logs
BULK INSERT LogData
FROM '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson'
WITH (FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt')