Ejemplo n.º 1
0
        public async override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (actionContext.ModelState.IsValid == false)
            {
                string __loggingResourceName = "Validation action filter";
                var service = new LoggingService();
        
                try
                {
                    //generate base log

                    var baseLog = service.GenerateBaseLog(ApiLogType.Warning, "ValidationFilter - Model not valid",
                        GetType(), service.GetCaller(),
                        ConfigurationManager.ConnectionStrings["AutoResolveContext"].ConnectionString);


                    var fullLog = await service.GenerateLog(actionContext.Request, actionContext.Response, baseLog);
                    service.WriteLog(baseLog.LoggingType, fullLog, baseLog.Exception);
                }
                catch (Exception ex)
                {
                    var errorLog =
                        await service.GenerateLog(actionContext.Request, actionContext.Response, new ApiBaseLog { LoggingType = ApiLogType.Fatal, Exception = ex, CurrentClass = this.GetType(), CurrentMethod = "", Message = "There has been a fatal error and logs cannot be generated" });
                    service.WriteLog(ApiLogType.Fatal, errorLog, ex);
                }

                actionContext.Response = actionContext.Request.CreateErrorResponse(
                            HttpStatusCode.BadRequest, actionContext.ModelState);
            }
        }
Ejemplo n.º 2
0
 public BaseController(AutoResolveContext db)
 {
     __db = db;
     _loggingService = new LoggingService();
     InitLogger();
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Uses serilog currently
        /// </summary>
        /// <returns></returns>
       /* protected virtual async Task CreateLog(ApiLogType logType, int responseCode, Type currentClass = null, string currentMethod = "", string message = "")
        {

            HttpRequestMessage httpRequestMessage =
                Request;

            var connectionString = __db.Database.Connection.ConnectionString;
            var contentType = Request.Content.Headers.ContentType.MediaType;
            var content = await Request.Content.ReadAsStringAsync();
            var routeDataasString = "";
            var headersasString = "";

            /*  foreach (var header in Request.Headers)
              {
                  var theStringToCompile  = "Header:" + header.Key + " " + "Value:" + header.Value;

                 /* foreach (var val in header.Value)
                  {
                      theStringToCompile += val + " ";
                  }#2#

                  headersasString += theStringToCompile;
              }
  #1#
            /*
                        foreach (var pair in Request.GetQueryNameValuePairs())
                        {
                             routeDataasString += "Key:" + pair.Key + " Values:" + pair.Value;

                        }#1#

            if (httpRequestMessage != null)
            {
                var user = httpRequestMessage.GetRequestContext().Principal as ClaimsPrincipal;
                //create the log
                var logEntry = new ApiLogEntry
                {
                    Application = Request.Headers.UserAgent.ToString(),
                    DatabaseConnectionString = connectionString,
                    RequestIpAddress = HttpContext.Current.Request.UserHostAddress ?? "",
                    RequestMethod = httpRequestMessage.Method.Method ?? "",
                    RequestUri = Request.RequestUri.AbsoluteUri ?? "",
                    ResponseStatusCode = responseCode,
                    Machine = HttpContext.Current.Request.UserHostName ?? "",
                    AuthorizationType = httpRequestMessage.Headers.Authorization.Scheme ?? "",
                    RequestContentType = Request.Content.Headers.ContentType.MediaType ?? "",
                    RequestContentBody = content ?? "",
                    RequestRouteData = routeDataasString ?? "",
                    RequestHeaders = headersasString ?? "",
                    RequestTimestamp = DateTime.Now,
                    Message = message ?? "",
                    CallingClass = string.Format("{0}", currentClass),
                    CallingMethod = currentMethod,
                    //RESPONSE
                    ResponseTimestamp = DateTime.Now,


                };


                if (user != null)
                {
                    logEntry.User = User.Identity.GetUserId();
                    logEntry.UserName = user.Identity.Name.Trim().ToLower();
                }
                else
                {
                    logEntry.User = Guid.Empty.ToString();
                    logEntry.UserName = "******";
                }



                switch (logType)
                {
                    case ApiLogType.Information:
                        Log.Logger.Information(message + " {@ApiLogEntry}", logEntry);
                        break;
                    case ApiLogType.Error:
                        Log.Logger.Error(message + " {@ApiLogEntry}", logEntry);
                        break;
                    case ApiLogType.Warning:
                        Log.Logger.Warning(message + " {@ApiLogEntry}", logEntry);
                        break;
                    case ApiLogType.Fatal:
                        Log.Logger.Fatal(message + " {@ApiLogEntry}", logEntry);
                        break;
                    case ApiLogType.Debug:
                        Log.Logger.Debug(message + " {@ApiLogEntry}", logEntry);
                        break;
                    case ApiLogType.Verbose:
                        Log.Logger.Verbose(message + " {@ApiLogEntry}", logEntry);
                        break;

                }


                Log.CloseAndFlush();
            }


        }*/

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            Log.Logger = null;
            _loggingService = null;
        }