From 037dcc75cd68126ca7cd55bb962fb30badf4730e Mon Sep 17 00:00:00 2001
From: utilcore <8907060@qq.com>
Date: Tue, 19 Sep 2017 00:40:53 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0exceptionless=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E6=93=8D=E4=BD=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/HomeController.cs | 38 +++++++++++++--
sample/Util.Samples.Webs/Startup.cs | 6 +--
src/Util.Logs/Contents/LogContent.cs | 2 +-
.../Exceptionless/ExceptionlessProvider.cs | 17 ++++---
src/Util.Logs/Exceptionless/ILogConvert.cs | 2 +-
src/Util.Logs/Exceptionless/LogContent.cs | 46 +++++++++----------
src/Util.Logs/Extensions/Extensions.Log.cs | 1 +
7 files changed, 73 insertions(+), 39 deletions(-)
diff --git a/sample/Util.Samples.Webs/Controllers/HomeController.cs b/sample/Util.Samples.Webs/Controllers/HomeController.cs
index 2f2df637..14ad61ad 100644
--- a/sample/Util.Samples.Webs/Controllers/HomeController.cs
+++ b/sample/Util.Samples.Webs/Controllers/HomeController.cs
@@ -17,19 +17,49 @@ namespace Util.Samples.Webs.Controllers {
public ILog Log { get; set; }
public void Index() {
+
try {
Throw();
}
catch ( Exception e ) {
- for ( int i = 0; i < 1000; i++ ) {
- Log.Exception( e ).BusinessId( "111" ).Error();
- }
+ Log.BusinessId( Guid.NewGuid().ToString() )
+ .Module( "订单" )
+ .ParamsLine( "{0},{1}", 1, 2 )
+ .Params( "{0},{1}", 3, 4 )
+ .Caption( "有人上班" )
+ .ContentLine( "购买商品数量: {0}", 100 )
+ .Content( "购买商品总额:{0}", 200 )
+ .SqlLine( "select * from system.users" )
+ .Sql( "select * from system.roles" )
+ .SqlParamsLine( "@a={0},@b={1}", 1, 2 )
+ .SqlParams( "@userId={0}", Guid.NewGuid().ToString() )
+ .Exception(e)
+ .Error();
}
}
private void Throw() {
- throw new Exception( "嘿嘿" );
+ try {
+ Throw2();
+ }
+ catch( Exception e ) {
+ throw new NotImplementedException( "Throw",e );
+ }
+ }
+
+ private void Throw2() {
+ try {
+ Throw3();
+ }
+ catch ( Exception e ) {
+ throw new NotImplementedException( "Throw2",e );
+ }
+
+ }
+
+ private void Throw3() {
+ throw new NotImplementedException( "Throw3" );
}
}
}
diff --git a/sample/Util.Samples.Webs/Startup.cs b/sample/Util.Samples.Webs/Startup.cs
index 3c759eec..aef209db 100644
--- a/sample/Util.Samples.Webs/Startup.cs
+++ b/sample/Util.Samples.Webs/Startup.cs
@@ -1,5 +1,4 @@
using System;
-using Exceptionless;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
@@ -38,8 +37,8 @@ namespace Util.Samples.Webs {
//添加Exceptionless日志操作
services.AddExceptionless( config => {
config.ServerUrl = "http://localhost:8011";
- //config.ApiKey = "oGBxMBfTQhdRJm1npjGgN1kNJvR6eYSWIpws8pvm";
- config.ApiKey = "IeOvbGtaG2Xk5twBRPac0HmPyJjfITXV4WVHO31o";
+ config.ApiKey = "oGBxMBfTQhdRJm1npjGgN1kNJvR6eYSWIpws8pvm";
+ //config.ApiKey = "IeOvbGtaG2Xk5twBRPac0HmPyJjfITXV4WVHO31o";
} );
//添加Ioc服务
@@ -60,7 +59,6 @@ namespace Util.Samples.Webs {
/// 配置异常处理
///
private void ConfigExceptionHandler( IApplicationBuilder app, IHostingEnvironment env ) {
- app.UseExceptionless();
if( env.IsDevelopment() ) {
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
diff --git a/src/Util.Logs/Contents/LogContent.cs b/src/Util.Logs/Contents/LogContent.cs
index e001bedb..1d20f658 100644
--- a/src/Util.Logs/Contents/LogContent.cs
+++ b/src/Util.Logs/Contents/LogContent.cs
@@ -118,7 +118,7 @@ namespace Util.Logs.Contents {
///
public string ErrorCode { get; set; }
///
- /// 异常消息
+ /// 异常
///
public Warning Exception { get; set; }
}
diff --git a/src/Util.Logs/Exceptionless/ExceptionlessProvider.cs b/src/Util.Logs/Exceptionless/ExceptionlessProvider.cs
index 93967c75..4bd8a845 100644
--- a/src/Util.Logs/Exceptionless/ExceptionlessProvider.cs
+++ b/src/Util.Logs/Exceptionless/ExceptionlessProvider.cs
@@ -1,4 +1,5 @@
-using Exceptionless;
+using System.Linq;
+using Exceptionless;
using Microsoft.Extensions.Logging;
using el = Exceptionless;
using Util.Logs.Abstractions;
@@ -31,12 +32,12 @@ namespace Util.Logs.Exceptionless {
///
/// 调试级别是否启用
///
- public bool IsDebugEnabled { get; }
+ public bool IsDebugEnabled => true;
///
/// 跟踪级别是否启用
///
- public bool IsTraceEnabled { get; }
+ public bool IsTraceEnabled => true;
///
/// 写日志
@@ -63,7 +64,8 @@ namespace Util.Logs.Exceptionless {
///
/// 日志内容
private string GetMessage( ILogContent content ) {
- if( content is ICaption caption )
+ var caption = content as ICaption;
+ if ( caption != null && string.IsNullOrWhiteSpace( caption.Caption ) == false )
return caption.Caption;
if( content.Content.Length > 0 )
return content.Content.ToString();
@@ -98,8 +100,11 @@ namespace Util.Logs.Exceptionless {
private void AddProperties( EventBuilder builder, ILogConvert content ) {
if ( content == null )
return;
- foreach ( var parameter in content.To() )
- builder.SetProperty( parameter.Key, parameter.Value );
+ foreach ( var parameter in content.To().OrderBy( t => t.SortId ) ) {
+ if ( string.IsNullOrWhiteSpace( parameter.Value ) )
+ continue;
+ builder.SetProperty( $"{Util.Helpers.Const.Letters[parameter.SortId-1]}. {parameter.Text}", parameter.Value );
+ }
}
}
}
diff --git a/src/Util.Logs/Exceptionless/ILogConvert.cs b/src/Util.Logs/Exceptionless/ILogConvert.cs
index a8f2a2f2..35f75383 100644
--- a/src/Util.Logs/Exceptionless/ILogConvert.cs
+++ b/src/Util.Logs/Exceptionless/ILogConvert.cs
@@ -8,6 +8,6 @@ namespace Util.Logs.Exceptionless {
///
/// 转换
///
- IDictionary To();
+ List- To();
}
}
diff --git a/src/Util.Logs/Exceptionless/LogContent.cs b/src/Util.Logs/Exceptionless/LogContent.cs
index 982f6902..99c5355c 100644
--- a/src/Util.Logs/Exceptionless/LogContent.cs
+++ b/src/Util.Logs/Exceptionless/LogContent.cs
@@ -8,29 +8,29 @@ namespace Util.Logs.Exceptionless {
///
/// 转换
///
- public IDictionary To() {
- return new Dictionary {
- { LogResource.LogName, LogName },
- { LogResource.TraceId, TraceId },
- { LogResource.OperationTime, OperationTime },
- { LogResource.Duration, Duration },
- { LogResource.ThreadId, ThreadId },
- { "Url", Url },
- { LogResource.UserId, UserId },
- { LogResource.Operator, Operator },
- { LogResource.Role,Role },
- { LogResource.BusinessId,BusinessId },
- { LogResource.Tenant,Tenant },
- { LogResource.Application,Application },
- { LogResource.Module,Module },
- { LogResource.Class,Class },
- { LogResource.Method,Method },
- { LogResource.Params,Params.ToString() },
- { LogResource.Caption,Caption },
- { LogResource.Content,Content.ToString() },
- { LogResource.Sql,Sql.ToString() },
- { LogResource.SqlParams,SqlParams.ToString() },
- { LogResource.ErrorCode,Exception.Code }
+ public List
- To() {
+ return new List
- {
+ { new Item( LogResource.LogName, LogName,1) },
+ { new Item(LogResource.TraceId, TraceId,2) },
+ { new Item(LogResource.OperationTime, OperationTime,3) },
+ { new Item(LogResource.Duration, Duration,4) },
+ { new Item(LogResource.ThreadId, ThreadId,5) },
+ { new Item("Url", Url,6) },
+ { new Item(LogResource.UserId, UserId,7) },
+ { new Item(LogResource.Operator, Operator,8 ) },
+ { new Item(LogResource.Role,Role,9) },
+ { new Item(LogResource.BusinessId,BusinessId,10) },
+ { new Item(LogResource.Tenant,Tenant,11) },
+ { new Item(LogResource.Application,Application,12) },
+ { new Item(LogResource.Module,Module ,13) },
+ { new Item(LogResource.Class,Class ,14) },
+ { new Item(LogResource.Method,Method ,15) },
+ { new Item(LogResource.Params,Params.ToString(),16) },
+ { new Item(LogResource.Caption,Caption ,17) },
+ { new Item(LogResource.Content,Content.ToString(),18) },
+ { new Item(LogResource.Sql,Sql.ToString(),19) },
+ { new Item(LogResource.SqlParams,SqlParams.ToString(),20) },
+ { new Item(LogResource.ErrorCode,Exception?.Code,21) }
};
}
}
diff --git a/src/Util.Logs/Extensions/Extensions.Log.cs b/src/Util.Logs/Extensions/Extensions.Log.cs
index 917fa3f1..e0e93f03 100644
--- a/src/Util.Logs/Extensions/Extensions.Log.cs
+++ b/src/Util.Logs/Extensions/Extensions.Log.cs
@@ -138,6 +138,7 @@ namespace Util.Logs.Extensions {
return log.Set( content => {
content.ErrorCode = exception.Code;
content.Exception = exception;
+ content.ExceptionMessage = exception.Message;
} );
}
}