-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Serilog package, configure sqlserver and Seq
- Loading branch information
htrlq
committed
Nov 26, 2018
1 parent
54b058e
commit 7e1d0d4
Showing
17 changed files
with
510 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
|
||
namespace LoggerUtil | ||
{ | ||
public class ColumInformat | ||
{ | ||
public Type DataType { get; set; } | ||
public string Property { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using Serilog.Events; | ||
|
||
namespace LoggerUtil | ||
{ | ||
public interface ISeriLogger | ||
{ | ||
string RootProperty { get; } | ||
|
||
void Write<T>(LogEventLevel level, string messageTemplate, T propertyValue); | ||
|
||
void Error(Exception ex, string messageTemplate); | ||
|
||
void Fatal(Exception ex, string messageTemplate); | ||
|
||
void Debug(Exception ex, string messageTemplate); | ||
|
||
void Information(Exception ex, string messageTemplate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using Serilog.Core; | ||
|
||
namespace LoggerUtil | ||
{ | ||
public class LoggerBuild | ||
{ | ||
private ConcurrentDictionary<Type, Logger> dictionary = new ConcurrentDictionary<Type, Logger>(); | ||
|
||
public bool ContainsKey(Type type) | ||
{ | ||
return dictionary.ContainsKey(type); | ||
} | ||
|
||
public bool Add(Type type, Logger logger) | ||
{ | ||
return dictionary.TryAdd(type, logger); | ||
} | ||
|
||
public Logger this[Type key] => dictionary[key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<RootNamespace>_1_Util</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="AspectCore.Extensions.DependencyInjection" Version="1.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.1.1" /> | ||
<PackageReference Include="Serilog" Version="2.7.1" /> | ||
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="5.1.2" /> | ||
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Reference Include="Microsoft.Extensions.Configuration.Abstractions"> | ||
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.configuration.abstractions\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.Extensions.Logging.Abstractions"> | ||
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.logging.abstractions\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll</HintPath> | ||
</Reference> | ||
<Reference Include="Microsoft.Extensions.Options"> | ||
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.extensions.options\2.1.1\lib\netstandard2.0\Microsoft.Extensions.Options.dll</HintPath> | ||
</Reference> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace LoggerUtil | ||
{ | ||
public static class SeriLogExtension | ||
{ | ||
public static IServiceCollection AddSeriLogger(this IServiceCollection serviceCollection, | ||
Action<SeriLoggerConfigure> action) | ||
{ | ||
serviceCollection.Configure(action); | ||
serviceCollection.AddSeriLogger(); | ||
|
||
return serviceCollection; | ||
} | ||
|
||
public static IServiceCollection AddSeriLogger(this IServiceCollection serviceCollection, | ||
IConfigurationSection section) | ||
{ | ||
serviceCollection.Configure<SeriLoggerConfigure>(section); | ||
serviceCollection.AddSeriLogger(); | ||
|
||
return serviceCollection; | ||
} | ||
|
||
public static IServiceCollection AddSeriLogger(this IServiceCollection serviceCollection) | ||
{ | ||
serviceCollection.AddSingleton<TableBuild>(); | ||
serviceCollection.AddSingleton<LoggerBuild>(); | ||
serviceCollection.AddScoped<ISeriLogger,SeriLogger>(); | ||
|
||
return serviceCollection; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
using Microsoft.Extensions.Options; | ||
using Serilog; | ||
using Serilog.Context; | ||
using Serilog.Core.Enrichers; | ||
using Serilog.Events; | ||
using Serilog.Sinks.MSSqlServer; | ||
using System; | ||
using System.Data; | ||
using Serilog.Core; | ||
|
||
namespace LoggerUtil | ||
{ | ||
|
||
public class SeriLogger: ISeriLogger | ||
{ | ||
public static readonly LoggerConfiguration Instance = new LoggerConfiguration(); | ||
|
||
private LoggerBuild LoggerBuild { get; } | ||
private TableBuild TableBuild { get; } | ||
private SeriLoggerConfigure Options { get; } | ||
|
||
public SeriLogger(IOptions<SeriLoggerConfigure> options, TableBuild build, LoggerBuild loggerBuild) | ||
{ | ||
TableBuild = build; | ||
LoggerBuild = loggerBuild; | ||
Options = options.Value; | ||
} | ||
|
||
public virtual string RootProperty => "SourceProperty"; | ||
|
||
public void Write<T>(LogEventLevel level, string messageTemplate, T propertyValue) | ||
{ | ||
var logger = Build<T>(); | ||
var type = typeof(T); | ||
var list = TableBuild[type]; | ||
|
||
var entity = list.ConvertAll(item => | ||
{ | ||
var propertyInfo = type.GetProperty(item.Property); | ||
var value = propertyInfo?.GetValue(propertyValue); | ||
return new PropertyEnricher(item.Property, value); | ||
}); | ||
|
||
using (LogContext.Push(entity.ToArray())) | ||
logger?.Write(level, messageTemplate, propertyValue); | ||
} | ||
|
||
private Logger Build<T>() | ||
{ | ||
var type = typeof(T); | ||
Logger logger; | ||
|
||
if (!TableBuild.ContainsKey(type)) | ||
{ | ||
var addNewTable = TableBuild.Add(type); | ||
|
||
if (!addNewTable) | ||
throw new TypeInitializationException(typeof(T).FullName, new Exception("Add New Table Error")); | ||
|
||
var isObject = typeof(Object) == type; | ||
|
||
if (Options.IsSeq) | ||
{ | ||
logger = Instance? | ||
.MinimumLevel.Verbose() | ||
//.Enrich.WithProperty(RootProperty, null) | ||
.Enrich.FromLogContext() | ||
.WriteTo.Seq(Options.SeqUrl) | ||
.CreateLogger(); | ||
} | ||
else | ||
{ | ||
ColumnOptions columnOptions; | ||
if (!isObject) | ||
{ | ||
columnOptions = new ColumnOptions() | ||
{ | ||
AdditionalDataColumns = TableBuild[type].ConvertAll(item => new DataColumn() | ||
{ DataType = item.DataType, ColumnName = item.Property }) | ||
}; | ||
|
||
columnOptions.Store.Add(StandardColumn.LogEvent); | ||
} | ||
else | ||
{ | ||
columnOptions = new ColumnOptions(); | ||
} | ||
|
||
logger = Instance? | ||
.MinimumLevel.Verbose() | ||
.Enrich.FromLogContext() | ||
.WriteTo.MSSqlServer(connectionString: Options.ConnectString, tableName: type.Name, restrictedToMinimumLevel: LogEventLevel.Debug, formatProvider: null, autoCreateSqlTable: true, columnOptions: columnOptions) | ||
.CreateLogger(); | ||
} | ||
|
||
var addNewLogger = LoggerBuild.Add(type, logger); | ||
|
||
if (!addNewLogger) | ||
throw new TypeInitializationException(typeof(T).FullName, new Exception("Add New Logger Error")); | ||
} | ||
else | ||
{ | ||
logger = LoggerBuild[type]; | ||
} | ||
|
||
return logger; | ||
} | ||
|
||
public void Error(Exception ex, string messageTemplate = "") | ||
{ | ||
var logger = Build<Object>(); | ||
logger.Error(ex, messageTemplate); | ||
} | ||
|
||
public void Fatal(Exception ex, string messageTemplate = "") | ||
{ | ||
var logger = Build<Object>(); | ||
logger.Fatal(ex, messageTemplate); | ||
} | ||
|
||
public void Debug(Exception ex, string messageTemplate = "") | ||
{ | ||
var logger = Build<Object>(); | ||
logger.Debug(ex, messageTemplate); | ||
} | ||
|
||
public void Information(Exception ex, string messageTemplate = "") | ||
{ | ||
var logger = Build<Object>(); | ||
logger.Information(ex, messageTemplate); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace LoggerUtil | ||
{ | ||
public class SeriLoggerConfigure | ||
{ | ||
public bool IsSeq => !string.IsNullOrWhiteSpace(SeqUrl); | ||
public string SeqUrl { get; set; } | ||
public bool IsSqlServer => !string.IsNullOrWhiteSpace(ConnectString); | ||
public string ConnectString { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using Serilog.Sinks.MSSqlServer; | ||
|
||
namespace LoggerUtil | ||
{ | ||
public class TableBuild | ||
{ | ||
private ConcurrentDictionary<Type,List<ColumInformat>> dictionary = new ConcurrentDictionary<Type, List<ColumInformat>>(); | ||
|
||
public bool ContainsKey(Type type) | ||
{ | ||
return dictionary.ContainsKey(type); | ||
} | ||
|
||
public bool Add(Type type) | ||
{ | ||
var list = new List<ColumInformat>(); | ||
foreach (var propertyInfo in type.GetProperties()) | ||
{ | ||
list.Add( | ||
new ColumInformat() | ||
{ | ||
DataType = propertyInfo.PropertyType, | ||
Property = propertyInfo.Name | ||
} | ||
); | ||
} | ||
|
||
return dictionary.TryAdd(type, list); | ||
} | ||
|
||
public List<ColumInformat> this[Type key] => dictionary[key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio 15 | ||
VisualStudioVersion = 15.0.28307.106 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication1", "WebApplication1\WebApplication1.csproj", "{E14E177F-BC12-427E-8D89-F6D40A1E576A}" | ||
EndProject | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LoggerUtil", "1-Util\LoggerUtil.csproj", "{E9DA1B1B-7D69-4D37-8D4F-58D754CF8067}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{E14E177F-BC12-427E-8D89-F6D40A1E576A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E14E177F-BC12-427E-8D89-F6D40A1E576A}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E14E177F-BC12-427E-8D89-F6D40A1E576A}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E14E177F-BC12-427E-8D89-F6D40A1E576A}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{E9DA1B1B-7D69-4D37-8D4F-58D754CF8067}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{E9DA1B1B-7D69-4D37-8D4F-58D754CF8067}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{E9DA1B1B-7D69-4D37-8D4F-58D754CF8067}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{E9DA1B1B-7D69-4D37-8D4F-58D754CF8067}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {65D6B665-659F-408E-90D4-85125B521D61} | ||
EndGlobalSection | ||
EndGlobal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using LoggerUtil; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Serilog.Events; | ||
using System.Collections.Generic; | ||
|
||
namespace WebApplication1.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class ValuesController : ControllerBase | ||
{ | ||
private ISeriLogger Log { get; } | ||
|
||
public ValuesController(ISeriLogger log) | ||
{ | ||
Log = log; | ||
} | ||
|
||
// GET api/values | ||
[HttpGet] | ||
public ActionResult<IEnumerable<string>> Get() | ||
{ | ||
Log.Write(LogEventLevel.Debug, "", new UserModel() | ||
{ | ||
User = "admin", | ||
Account = "system" | ||
}); | ||
return new [] { "value1", "value2" }; | ||
} | ||
} | ||
|
||
public class UserModel | ||
{ | ||
public string User { get; set; } | ||
public string Account { get; set; } | ||
} | ||
} |
Oops, something went wrong.