Skip to content

Commit

Permalink
Merge pull request #1 from htrlq/dev
Browse files Browse the repository at this point in the history
merge dev to master
  • Loading branch information
htrlq authored Nov 26, 2018
2 parents c02110f + 7e580e9 commit 4ea2a88
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 65 deletions.
8 changes: 4 additions & 4 deletions 1-Util/ISeriLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public interface ISeriLogger

void Write<T>(LogEventLevel level, string messageTemplate, T propertyValue);

void Error(Exception ex, string messageTemplate);
void Error(Exception ex, string messageTemplate = "");

void Fatal(Exception ex, string messageTemplate);
void Fatal(Exception ex, string messageTemplate = "");

void Debug(Exception ex, string messageTemplate);
void Debug(Exception ex, string messageTemplate = "");

void Information(Exception ex, string messageTemplate);
void Information(Exception ex, string messageTemplate = "");
}
}
26 changes: 11 additions & 15 deletions 1-Util/SeriLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using System;
using System.Collections.Generic;
using System.Data;
using Serilog.Core;

Expand All @@ -13,8 +14,6 @@ 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; }
Expand Down Expand Up @@ -48,7 +47,7 @@ public void Write<T>(LogEventLevel level, string messageTemplate, T propertyValu
private Logger Build<T>()
{
var type = typeof(T);
Logger logger;
Logger logger;

if (!TableBuild.ContainsKey(type))
{
Expand All @@ -57,11 +56,12 @@ private Logger Build<T>()
if (!addNewTable)
throw new TypeInitializationException(typeof(T).FullName, new Exception("Add New Table Error"));

var isObject = typeof(Object) == type;
var isObject = typeof(Exception) == type;
var instance = new LoggerConfiguration();

if (Options.IsSeq)
{
logger = Instance?
logger = instance?
.MinimumLevel.Verbose()
//.Enrich.WithProperty(RootProperty, null)
.Enrich.FromLogContext()
Expand All @@ -70,7 +70,7 @@ private Logger Build<T>()
}
else
{
ColumnOptions columnOptions;
ColumnOptions columnOptions = null;
if (!isObject)
{
columnOptions = new ColumnOptions()
Expand All @@ -81,12 +81,8 @@ private Logger Build<T>()

columnOptions.Store.Add(StandardColumn.LogEvent);
}
else
{
columnOptions = new ColumnOptions();
}

logger = Instance?
logger = instance?
.MinimumLevel.Verbose()
.Enrich.FromLogContext()
.WriteTo.MSSqlServer(connectionString: Options.ConnectString, tableName: type.Name, restrictedToMinimumLevel: LogEventLevel.Debug, formatProvider: null, autoCreateSqlTable: true, columnOptions: columnOptions)
Expand All @@ -108,25 +104,25 @@ private Logger Build<T>()

public void Error(Exception ex, string messageTemplate = "")
{
var logger = Build<Object>();
var logger = Build<Exception>();
logger.Error(ex, messageTemplate);
}

public void Fatal(Exception ex, string messageTemplate = "")
{
var logger = Build<Object>();
var logger = Build<Exception>();
logger.Fatal(ex, messageTemplate);
}

public void Debug(Exception ex, string messageTemplate = "")
{
var logger = Build<Object>();
var logger = Build<Exception>();
logger.Debug(ex, messageTemplate);
}

public void Information(Exception ex, string messageTemplate = "")
{
var logger = Build<Object>();
var logger = Build<Exception>();
logger.Information(ex, messageTemplate);
}
}
Expand Down
77 changes: 77 additions & 0 deletions WebApplication1/Controllers/SerilogController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using LoggerUtil;
using Microsoft.AspNetCore.Mvc;
using Serilog.Events;
using System.Collections.Generic;

namespace WebApplication1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class SerilogController : ControllerBase
{
private ISeriLogger Log { get; }

public SerilogController(ISeriLogger log)
{
Log = log;
}

// GET api/values
[HttpGet]
public string Write()
{
Log.Write(LogEventLevel.Debug, "", new UserModel()
{
User = "admin",
Account = "system"
});
return "Write";
}

private Exception Excepts =
new Exception("node", new Exception("Node Parent", new Exception("Node Parent Parent")));

[HttpGet("Error")]
public string Error()
{
Log.Error(Excepts);
return "Error";
}

[HttpGet("Fatal")]
public string Fatal()
{
Log.Fatal(Excepts);
return "Fatal";
}

[HttpGet("Debug")]
public string Debug()
{
Log.Debug(Excepts);
return "Debug";
}

[HttpGet("Information")]
public string Information()
{
Log.Information(Excepts);
return "Information";
}

[ResponseException]
[Logger]
[HttpGet("TestException")]
public void TestException()
{
throw Excepts;
}
}

public class UserModel
{
public string User { get; set; }
public string Account { get; set; }
}
}
37 changes: 0 additions & 37 deletions WebApplication1/Controllers/ValuesController.cs

This file was deleted.

16 changes: 16 additions & 0 deletions WebApplication1/LoggerFilrter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using LoggerUtil;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication1
{
public class LoggerAttribute: ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext context)
{
var logger = context.HttpContext.RequestServices.GetRequiredService(typeof(ISeriLogger)) as ISeriLogger;
logger.Error(context.Exception.InnerException);
}
}
}
14 changes: 7 additions & 7 deletions WebApplication1/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:55739",
"sslPort": 44341
}
},
"$schema": "http://json.schemastore.org/launchsettings.json",
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "api/values",
"launchUrl": "api/Serilog",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
Expand All @@ -21,10 +21,10 @@
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
14 changes: 14 additions & 0 deletions WebApplication1/ResponseException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace WebApplication1
{
public class ResponseException : ActionFilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext context)
{
context.Result = new JsonResult(new ResponseModel(){ Error = "错误" });
}
}
}
11 changes: 11 additions & 0 deletions WebApplication1/ResponseModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace WebApplication1
{
public class ResponseModel
{
public bool IsError => !string.IsNullOrWhiteSpace(Error);

public string Error { get; set; }

public object Result { get; set; } = null;
}
}
4 changes: 2 additions & 2 deletions WebApplication1/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddSeriLogger(obj =>
{
//obj.SeqUrl = "http://127.0.0.1:5341";
obj.ConnectString = "server=127.0.0.1,1401;database=Serilog;uid=sa;pwd=hTrlq20181123!;";
obj.SeqUrl = "http://127.0.0.1:5341";
//obj.ConnectString = "server=127.0.0.1,1401;database=Serilog;uid=sa;pwd=hTrlq20181123!;";
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Expand Down

0 comments on commit 4ea2a88

Please sign in to comment.