更新exceptionless日志操作

This commit is contained in:
utilcore
2017-09-19 00:40:53 +08:00
parent 56ba4c3b47
commit 037dcc75cd
7 changed files with 73 additions and 39 deletions
@@ -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" );
}
}
}
+2 -4
View File
@@ -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 {
/// 配置异常处理
/// </summary>
private void ConfigExceptionHandler( IApplicationBuilder app, IHostingEnvironment env ) {
app.UseExceptionless();
if( env.IsDevelopment() ) {
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
+1 -1
View File
@@ -118,7 +118,7 @@ namespace Util.Logs.Contents {
/// </summary>
public string ErrorCode { get; set; }
/// <summary>
/// 异常消息
/// 异常
/// </summary>
public Warning Exception { get; set; }
}
@@ -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 {
/// <summary>
/// 调试级别是否启用
/// </summary>
public bool IsDebugEnabled { get; }
public bool IsDebugEnabled => true;
/// <summary>
/// 跟踪级别是否启用
/// </summary>
public bool IsTraceEnabled { get; }
public bool IsTraceEnabled => true;
/// <summary>
/// 写日志
@@ -63,7 +64,8 @@ namespace Util.Logs.Exceptionless {
/// </summary>
/// <param name="content">日志内容</param>
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 );
}
}
}
}
+1 -1
View File
@@ -8,6 +8,6 @@ namespace Util.Logs.Exceptionless {
/// <summary>
/// 转换
/// </summary>
IDictionary<string, string> To();
List<Item> To();
}
}
+23 -23
View File
@@ -8,29 +8,29 @@ namespace Util.Logs.Exceptionless {
/// <summary>
/// 转换
/// </summary>
public IDictionary<string, string> To() {
return new Dictionary<string, string> {
{ 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<Item> To() {
return new List<Item> {
{ 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) }
};
}
}
@@ -138,6 +138,7 @@ namespace Util.Logs.Extensions {
return log.Set<LogContent>( content => {
content.ErrorCode = exception.Code;
content.Exception = exception;
content.ExceptionMessage = exception.Message;
} );
}
}