Exemplo n.º 1
0
        public static ErrorModel GetErrorModel(ExceptionContext context, IElectLog electLog)
        {
            ErrorModel errorModel;

            if (context.Exception is CoreException exception)
            {
                context.Exception = null;

                errorModel = new ErrorModel(exception.Code, exception.Message, exception.AdditionalData);

                // Log Business Logic Exception
                exception.Data.Add(nameof(errorModel.Id), errorModel.Id);
                electLog.Capture(exception, LogType.Warning, context.HttpContext);

                if (exception.AdditionalData?.Any() == true)
                {
                    errorModel.AdditionalData = exception.AdditionalData;
                }

                context.HttpContext.Response.StatusCode = errorModel.HttpResponseCode;
            }
            else if (context.Exception is UnauthorizedAccessException)
            {
                errorModel = new ErrorModel(ErrorCode.UnAuthorized, "UnAuthorized Access");

                context.HttpContext.Response.StatusCode = errorModel.HttpResponseCode;
            }
            else
            {
                var message =
                    EnvHelper.IsDevelopment()
                        ? context.Exception.Message
                        : "Oops! Something went wrong, please try again later";

                errorModel = new ErrorModel(ErrorCode.Unknown, message);

                // Log Code Logic Exception 
                context.Exception.Data.Add(nameof(errorModel.Id), errorModel.Id);
                electLog.Capture(context.Exception, LogType.Error, context.HttpContext);

                if (EnvHelper.IsDevelopment())
                {
                    // Add additional data
                    errorModel.AdditionalData.Add("stackTrace", context.Exception.StackTrace);
                    errorModel.AdditionalData.Add("innerException", context.Exception.InnerException?.Message);
                    errorModel.AdditionalData.Add("note",
                        "The message is exception message and additional data such as 'stackTrace', 'internalException' and 'note' only have in [Development Environment].");
                }

                context.HttpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
            }

            return errorModel;
        }
Exemplo n.º 2
0
        public IActionResult Index()
        {
            var deviceInfo = HttpContext.Request.GetDeviceInformation();

            _electLog.Capture("Message Sample");

            var filePath = "Special Path";

            _electLog.Capture("Message Sample Force File Path 1", jsonFilePath: filePath);

            _electLog.Capture("Message Sample Force File Path 2", jsonFilePath: filePath);

            return(View());
        }
Exemplo n.º 3
0
        public IActionResult Index()
        {
            var deviceInfo = HttpContext.Request.GetDeviceInformation();

            _electLog.Capture("Message Sample", LogType.Info);

            return(View());
        }
Exemplo n.º 4
0
        public static ErrorModel GetErrorModel(ExceptionContext context, IElectLog electLog)
        {
            ErrorModel errorModel;

            switch (context.Exception)
            {
            case CoreException exception:
                errorModel = new ErrorModel(exception);

                electLog.Capture(exception, LogType.Warning, context.HttpContext);

                break;

            case UnauthorizedAccessException _:
                errorModel = new ErrorModel(nameof(ErrorCode.UnAuthorized), ErrorCode.UnAuthorized, StatusCodes.Status401Unauthorized);

                break;

            default:
                var message = EnvHelper.IsDevelopment() ? context.Exception.Message : ErrorCode.Unknown;

                errorModel = new ErrorModel(nameof(ErrorCode.Unknown), message, StatusCodes.Status500InternalServerError);

                electLog.Capture(context.Exception, LogType.Error, context.HttpContext);

                if (EnvHelper.IsDevelopment())
                {
                    // Add additional data
                    errorModel.AdditionalData.Add("stackTrace", context.Exception.StackTrace);

                    errorModel.AdditionalData.Add("innerException", context.Exception.InnerException?.Message);

                    errorModel.AdditionalData.Add("note", "The message is exception message and additional data such as 'stackTrace', 'internalException' and 'note' only have in [Development Environment].");
                }

                break;
            }

            context.HttpContext.Response.StatusCode = errorModel.StatusCode;

            return(errorModel);
        }