Web Api – Exception Filter Attribute
Merhabalar ,
Bu yazımda api mizde hataları yakalamak için Exception Filter ın nasıl kullanılacağından bahsedeceğim , hataları birçok şekilde yakalayabiliriz örn , heryere try catch blok ları koyularakta bu işlemler yapılabilir , fakat bunun önüne geömek için microsoft ExceptionFilterAttribute sınıfını oluşturmuştur. Örneğimizi yaparken bu sınıfı kullanacağız .
Projemizde öncelik olarak kendi Filter ımızı oluşturacağız , adı MyErrorAttribute olsun ve ExceptionFilterAttribute sınıfından miras alsın , ve bu sınıfın onException metodunu ezip kendi istediğimiz işlemleri yaptıralım.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http.Filters;
namespace WebApiApp.Filters
{
public class MyErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//1. Loglama
//2. Response Oluşturma
}
}
}
Örnek olarak hata aldığında bu hatayı loglayalım , ve hata için response oluşturalım.
Örnek amaçlı Log işlemi için Logger.cs adınada bir sınıf oluşturdum , static bir LogYaz metodu olsun ,içeriğini kendi sisteminizin yapısına göre doldurabilirsiniz.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApiApp
{
public class Logger
{
public static void LogYaz(string message, string messageDetail)
{
//throw new NotImplementedException();
}
}
}

Hata mesajı içeriğini actionExecutedContext ten alıyoruz .
Response mesajı olarak belirlediğimiz bir tipte bir sınıfta bir hata response döndürmesi için kendi response sınıfımı oluşturuyorum.
MyErrorResponse.cs adında bir sınıf oluşturuyorum .
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApiApp
{
public class MyErrorResponse
{
public string ErrorMessage { get; set; }
public string ErrorAction { get; set; }
public string ErrorController { get; set; }
}
}
Oluşturduğumuz sınıfı response olarak kullanalım
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Filters;
namespace WebApiApp.Filters
{
public class MyErrorAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
//1. Loglama
Logger.LogYaz(actionExecutedContext.Exception.Message, actionExecutedContext.Exception.StackTrace);
//2. Response Oluşturma
MyErrorResponse result = new MyErrorResponse();
result.ErrorAction = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
result.ErrorController = actionExecutedContext.ActionContext.ControllerContext.ControllerDescriptor.ControllerName;
result.ErrorMessage = actionExecutedContext.Exception.ToString();
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, result);
}
}
}
Yazmış olduğumuz MyErrorAtrribute.cs in son hali .

Artık bu attribute ü istediğimiz controller veya action üzerine ekleyerek kullanabiliriz.
Örn :

Çalışmasını test etmek için throw ile hata fırlatalım.

Sonuç :

Bu yazımızın da sona geldik , hem not hem de hatırlatma olarak , umarım size de faydası olur.

