Skip to content

Commit 9685033

Browse files
committed
Belgrade dmeo upgrades to .net core 2.1 and added NLog
1 parent 649efa4 commit 9685033

9 files changed

Lines changed: 129 additions & 40 deletions

File tree

samples/demos/belgrade-product-catalog-demo/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ Properties/PublishProfiles/*
1010
appsettings.Development.json
1111
appsettings.Production.json
1212
*.ndjson
13-
sql-scripts/bcp.sql.sql
13+
*.tsjson
14+
logs/internal-nlog.txt
15+
sql-scripts/bcp.sql

samples/demos/belgrade-product-catalog-demo/Controllers/ProductCatalogController.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.Extensions.Logging;
23
using ProductCatalog.Models;
34
using System;
45
using System.Collections.Generic;
@@ -9,9 +10,11 @@ namespace ProductCatalog.Controllers
910
public class ProductCatalogController : Controller
1011
{
1112
private ProductCatalogContext _context;
12-
13-
public ProductCatalogController (ProductCatalogContext context)
13+
private readonly ILogger<ProductCatalogController> _logger;
14+
15+
public ProductCatalogController (ProductCatalogContext context, ILogger<ProductCatalogController> logger)
1416
{
17+
_logger = logger;
1518
_context = context;
1619
}
1720

samples/demos/belgrade-product-catalog-demo/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
3+
using NLog.Web;
34
using System.IO;
45

56
namespace ProductCatalog
@@ -12,6 +13,7 @@ public static void Main(string[] args)
1213
.UseKestrel()
1314
.UseContentRoot(Directory.GetCurrentDirectory())
1415
.UseIISIntegration()
16+
.UseNLog()
1517
.UseStartup<Startup>()
1618
.Build();
1719

samples/demos/belgrade-product-catalog-demo/Startup.cs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using Belgrade.SqlClient;
22
using Belgrade.SqlClient.SqlDb;
3-
using Belgrade.SqlClient.SqlDb.Rls;
43
using Microsoft.AspNetCore.Builder;
54
using Microsoft.AspNetCore.Hosting;
65
using Microsoft.AspNetCore.Http;
@@ -9,31 +8,35 @@
98
using Microsoft.Extensions.DependencyInjection;
109
using Microsoft.Extensions.Logging;
1110
using ProductCatalog.Models;
12-
using Serilog;
13-
using Serilog.Sinks;
11+
using Serilog;
1412
using Serilog.Sinks.MSSqlServer;
1513
using System;
1614
using System.Data.SqlClient;
1715
using System.Linq;
1816

17+
using NLog.Extensions.Logging;
18+
using NLog.Web;
19+
1920
namespace ProductCatalog
2021
{
2122
public class Startup
2223
{
2324
public Startup(IHostingEnvironment env)
2425
{
26+
env.ConfigureNLog("nlog.config");
27+
2528
var builder = new ConfigurationBuilder()
2629
.SetBasePath(env.ContentRootPath)
2730
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
2831
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
2932
.AddEnvironmentVariables();
3033
Configuration = builder.Build();
31-
#if NETCOREAPP2_0
32-
Log.Logger = new LoggerConfiguration()
33-
.WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
34-
.CreateLogger();
35-
#endif
36-
#if NET46
34+
35+
// Enable this if you want to log into local folder as newline-delimited JSON
36+
//Log.Logger = new LoggerConfiguration()
37+
// .WriteTo.RollingFile(new Serilog.Formatting.Json.JsonFormatter(), System.IO.Path.Combine(env.ContentRootPath, "logs\\log-{Date}.ndjson"))
38+
// .CreateLogger();
39+
3740
var columnOptions = new ColumnOptions();
3841
// Don't include the Properties XML column.
3942
columnOptions.Store.Remove(StandardColumn.Id);
@@ -47,7 +50,6 @@ public Startup(IHostingEnvironment env)
4750
Log.Logger = new LoggerConfiguration()
4851
.WriteTo.MSSqlServer(Configuration["ConnectionStrings:BelgradeDemo"], "Logs", columnOptions: columnOptions, autoCreateSqlTable: false)
4952
.CreateLogger();
50-
#endif
5153
}
5254

5355
public IConfigurationRoot Configuration { get; }
@@ -72,6 +74,7 @@ public void ConfigureServices(IServiceCollection services)
7274

7375
//// Add framework services.
7476
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
77+
7578
services.AddLogging();
7679
services.AddSession();
7780
services.AddMvc();
@@ -84,6 +87,11 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
8487
loggerFactory.AddDebug();
8588
loggerFactory.AddSerilog();
8689

90+
//add NLog to ASP.NET Core
91+
loggerFactory.AddNLog();
92+
NLog.LogManager.ThrowExceptions = true;
93+
NLog.LogManager.ThrowConfigExceptions = true;
94+
8795
app.UseSession();
8896
app.UseStaticFiles();
8997
app.UseMvc(routes =>

samples/demos/belgrade-product-catalog-demo/belgrade-product-catalog-demo.csproj

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<AssemblyName>belgrade-product-catalog-demo</AssemblyName>
66
<OutputType>Exe</OutputType>
77
<PackageId>belgrade-product-catalog-demo</PackageId>
8-
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
9-
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
10-
<TargetFrameworkProfile />
8+
<TargetFrameworks>netcoreapp2.1</TargetFrameworks>
9+
<AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
10+
<ApplicationIcon />
11+
<StartupObject />
1112
</PropertyGroup>
1213

1314
<ItemGroup>
@@ -18,35 +19,41 @@
1819

1920
<ItemGroup>
2021
<PackageReference Include="Belgrade.Sql.Client" Version="1.1.4" />
21-
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
22-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
23-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
24-
<PackageReference Include="Microsoft.AspNetCore.Session" Version="1.0.2" />
25-
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.2" />
26-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.0.3" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.2" />
28-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
29-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
30-
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
31-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
32-
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
33-
<PackageReference Include="Serilog" Version="2.5.0" />
34-
<PackageReference Include="Serilog.Extensions.Logging" Version="1.3.1" />
22+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.1.3" />
23+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="2.1.2" />
24+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.1.3" />
25+
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.1.1" />
26+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
27+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.4" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
29+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.1.1" />
30+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
31+
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
32+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.1" />
33+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.1.1" />
34+
<PackageReference Include="NLog" Version="4.5.10" />
35+
<PackageReference Include="NLog.Extensions.AzureStorage" Version="1.0.9" />
36+
<PackageReference Include="NLog.Web.AspNetCore" Version="4.7.0" />
37+
<PackageReference Include="Serilog" Version="2.7.1" />
38+
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
3539
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="2.1.1" />
3640
<PackageReference Include="Serilog.Sinks.RollingFile" Version="3.3.0" />
37-
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
41+
<PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
3842
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.3-dev-00224" />
3943
</ItemGroup>
4044

4145
<ItemGroup>
42-
<None Update="sql-scripts\bcp.sql.sql">
46+
<None Update="nlog.config">
47+
<CopyToOutputDirectory>Preserve Newest</CopyToOutputDirectory>
48+
</None>
49+
<None Update="sql-scripts\bcp.sql">
4350
<DesignTime>True</DesignTime>
4451
<AutoGen>True</AutoGen>
45-
<DependentUpon>bcp.sql.tt</DependentUpon>
52+
<DependentUpon>bcp.tt</DependentUpon>
4653
</None>
47-
<None Update="sql-scripts\bcp.sql.tt">
54+
<None Update="sql-scripts\bcp.tt">
4855
<Generator>TextTemplatingFileGenerator</Generator>
49-
<LastGenOutput>bcp.sql.sql</LastGenOutput>
56+
<LastGenOutput>bcp.sql</LastGenOutput>
5057
</None>
5158
</ItemGroup>
5259

samples/demos/belgrade-product-catalog-demo/logs/linedelimited.fmt renamed to samples/demos/belgrade-product-catalog-demo/logs/ndjson.fmt

File renamed without changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
12.0
2+
4
3+
1 SQLCHAR 0 30 "\t" 1 EventTime SQL_Latin1_General_CP1_CI_AS
4+
2 SQLCHAR 0 20 "\t" 2 Level SQL_Latin1_General_CP1_CI_AS
5+
3 SQLCHAR 0 4000 "\t" 3 Message SQL_Latin1_General_CP1_CI_AS
6+
4 SQLCHAR 0 80000 "\r\n" 4 EventData SQL_Latin1_General_CP1_CI_AS
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
autoReload="true"
5+
internalLogLevel="info"
6+
internalLogFile=".\logs\internal-nlog.txt">
7+
8+
<!-- enable asp.net core layout renderers -->
9+
<extensions>
10+
<add assembly="NLog.Web.AspNetCore"/>
11+
<add assembly="NLog.Extensions.AzureStorage"/>
12+
</extensions>
13+
14+
<!-- the targets to write to -->
15+
<targets>
16+
17+
<target xsi:type="File" name="file" fileName="${currentdir}\logs\log-${shortdate}.tsjson">
18+
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false" quoting="Nothing">
19+
<column name="time" layout="${longdate}" />
20+
<column name="level" layout="${level:upperCase=true}"/>
21+
<column name="message" layout="${message}" />
22+
<column name="data">
23+
<layout xsi:type="JsonLayout">
24+
<attribute name="callsite" layout="${callsite:includeSourcePath=true}" />
25+
<attribute name="stacktrace" layout="${stacktrace:topFrames=10}" />
26+
<attribute name="exception" layout="${exception:format=ToString}"/>
27+
</layout>
28+
</column>
29+
</layout>
30+
</target>
31+
32+
<target xsi:type="AzureBlobStorage"
33+
name="blob"
34+
blobName="log-${shortdate}.tsjson"
35+
container="logs"
36+
connectionString="DefaultEndpointsProtocol=https;AccountName=********;AccountKey=******************************;EndpointSuffix=core.windows.net"
37+
>
38+
<layout xsi:type="CsvLayout" delimiter="Tab" withHeader="false" quoting="Nothing">
39+
<column name="time" layout="${longdate}" />
40+
<column name="level" layout="${level:upperCase=true}"/>
41+
<column name="message" layout="${message}" />
42+
<column name="data">
43+
<layout xsi:type="JsonLayout">
44+
<attribute name="callsite" layout="${callsite:includeSourcePath=true}" />
45+
<attribute name="stacktrace" layout="${stacktrace:topFrames=10}" />
46+
<attribute name="exception" layout="${exception:format=ToString}"/>
47+
</layout>
48+
</column>
49+
</layout>
50+
</target>
51+
</targets>
52+
53+
<!-- rules to map from logger name to target -->
54+
<rules>
55+
<logger name="*" minlevel="Trace" writeTo="file" />
56+
<logger name="*" minlevel="Trace" writeTo="blob" />
57+
</rules>
58+
</nlog>

samples/demos/belgrade-product-catalog-demo/sql-scripts/bcp.sql.tt renamed to samples/demos/belgrade-product-catalog-demo/sql-scripts/bcp.tt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,24 @@
33

44
SELECT *
55
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson',
6-
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt' ) as logs;
7-
6+
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\ndjson.fmt' ) as logs;
87

8+
SELECT *
9+
FROM OPENROWSET(BULK '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.tsjson',
10+
FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\tsjson.fmt' ) as logs;
911

10-
DROP TABLE IF EXISTS Logs
12+
DROP TABLE IF EXISTS DataLog
1113

1214
--{"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 "}]}}
13-
CREATE TABLE Logs (
15+
CREATE TABLE LogData (
1416
Data NVARCHAR(MAX),
1517
Timestamp AS CAST(JSON_VALUE(Data, '$.Timestamp') as datetime2),
1618
Level AS CAST(JSON_VALUE(Data, '$.Level') as nvarchar(40)),
1719
ElapsedMilliseconds AS CAST(JSON_VALUE(Data, '$.Properties.ElapsedMilliseconds') AS float),
1820
StatusCode AS CAST(JSON_VALUE(Data, '$.Properties.StatusCode') AS int)
1921
)
2022

21-
BULK INSERT Logs
23+
BULK INSERT LogData
2224
FROM '<#=this.Host.ResolvePath("..\\logs") #>\log-20170203.ndjson'
2325
WITH (FORMATFILE = '<#=this.Host.ResolvePath("..\\logs") #>\linedelimited.fmt')
26+

0 commit comments

Comments
 (0)