.net webapi接入exceptionless
EL是微软官方推荐的异常处理第三方框架,比较成熟,具体可以到github或者EL官网了解,目前公司项目是微软技术栈 asp.net mvc wcf webapi ,EL对于mvc支持是很全面的实现了异常自动拦截,但是api却不是的,需要用户手动提交异常,这里说下配置过程
1.通过nuget安装 Exceptionless.WebApi以及Exceptionless
2.在WebApiConfig中添加
//注册EL异常处理
ExceptionlessClient.Default.RegisterWebApi(config);
3.在web.config中增加配置
<configSections>
<section name="exceptionless" type="Exceptionless.ExceptionlessSection, Exceptionless" />
</configSections>
<exceptionless apiKey="your key" serverUrl="your server url" enabled="true"></exceptionless>
<ParmLengthDictionary>
4.在OnException中加入
actionExecutedContext.Exception.ToExceptionless().Submit();
或者在try catch中记录
try
{
//dosomething
}
catch (Exception ex)
{
ex.ToExceptionless().Submit();
}
当然这只是基本的记录 还可以记录更多信息并且进行打标签分类等等,如果有需求可以继续增加,贴出来一部分接口
#region Assembly Exceptionless, Version=4.3.2004.0, Culture=neutral, PublicKeyToken=null
// F:\Test\CRSP.CUMCM\packages\Exceptionless.4.3.2004\lib\net45\Exceptionless.dll
#endregion
using System;
using System.Collections.Generic;
using Exceptionless.Models;
using Exceptionless.Plugins;
namespace Exceptionless
{
public class EventBuilder
{
public EventBuilder(Event ev, ExceptionlessClient client = null, ContextData pluginContextData = null);
public ExceptionlessClient Client { get; set; }
//
// Summary:
// Any contextual data objects to be used by Exceptionless plugins to gather additional
// information for inclusion in the event.
public ContextData PluginContextData { get; }
public Event Target { get; }
//
// Summary:
// Adds the object to extended data. Use either excludedPropertyNames or Exceptionless.Json.JsonIgnoreAttribute
// to exclude data from being included in the event.
//
// Parameters:
// data:
// The data object to add.
//
// name:
// The name of the object to add.
//
// maxDepth:
// The max depth of the object to include.
//
// excludedPropertyNames:
// Any property names that should be excluded.
//
// ignoreSerializationErrors:
// Specifies if properties that throw serialization errors should be ignored.
public EventBuilder AddObject(object data, string name = null, int? maxDepth = default(int?), ICollection<string> excludedPropertyNames = null, bool ignoreSerializationErrors = false);
//
// Summary:
// Adds one or more tags to the event.
//
// Parameters:
// tags:
// The tags to be added to the event.
public EventBuilder AddTags(params string[] tags);
//
// Summary:
// Marks the event as being a critical occurrence.
public EventBuilder MarkAsCritical();
//
// Summary:
// Allows you to reference a parent event by its Exceptionless.Models.Event.ReferenceId
// property. This allows you to have parent and child relationships.
//
// Parameters:
// name:
// Reference name
//
// id:
// The reference id that points to a specific event
public EventBuilder SetEventReference(string name, string id);
//
// Summary:
// Sets the event exception object.
//
// Parameters:
// ex:
// The exception
public EventBuilder SetException(Exception ex);
//
// Summary:
// Sets the event geo coordinates. Can be either "lat,lon" or an IP address that
// will be used to auto detect the geo coordinates.
//
// Parameters:
// coordinates:
// The event coordinates.
public EventBuilder SetGeo(string coordinates);
//
// Summary:
// Sets the event geo coordinates.
//
// Parameters:
// latitude:
// The event latitude.
//
// longitude:
// The event longitude.
public EventBuilder SetGeo(double latitude, double longitude);
//
// Summary:
// Sets the event message.
//
// Parameters:
// message:
// The event message.
public EventBuilder SetMessage(string message);
//
// Summary:
// Sets an extended property value to include with the event. Use either excludedPropertyNames
// or Exceptionless.Json.JsonIgnoreAttribute to exclude data from being included
// in the event report.
//
// Parameters:
// name:
// The name of the object to add.
//
// value:
// The data object to add.
//
// maxDepth:
// The max depth of the object to include.
//
// excludedPropertyNames:
// Any property names that should be excluded.
//
// ignoreSerializationErrors:
// Specifies if properties that throw serialization errors should be ignored.
public EventBuilder SetProperty(string name, object value, int? maxDepth = default(int?), ICollection<string> excludedPropertyNames = null, bool ignoreSerializationErrors = false);
//
// Summary:
// Sets the event reference id.
//
// Parameters:
// referenceId:
// The event reference id.
public EventBuilder SetReferenceId(string referenceId);
//
// Summary:
// Sets the event source.
//
// Parameters:
// source:
// The event source.
public EventBuilder SetSource(string source);
//
// Summary:
// Sets the event type.
//
// Parameters:
// type:
// The event type.
public EventBuilder SetType(string type);
//
// Summary:
// Sets the event value.
//
// Parameters:
// value:
// The value of the event.
public EventBuilder SetValue(decimal value);
//
// Summary:
// Submits the event report.
public void Submit();
}
}