private void SalvarLogDaApiNoBancoDeDados(LogApi logApi) { //string content = JsonConvert.SerializeObject(apiLogEntry); using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["MonitorConnectionString"].ConnectionString)) { connection.Execute( @"INSERT INTO LogApi (Id, Application, Machine, RequestContentBody, RequestContentType, RequestHeaders, RequestIpAddress, RequestMethod, RequestRouteData, RequestRouteTemplate, RequestTimestamp, RequestUri, ResponseContentBody, ResponseContentType, ResponseHeaders, ResponseStatusCode, ResponseTimestamp, [User]) VALUES (@Id, @Application, @Machine, @RequestContentBody, @RequestContentType, @RequestHeaders, @RequestIpAddress, @RequestMethod, @RequestRouteData, @RequestRouteTemplate, @RequestTimestamp, @RequestUri, @ResponseContentBody, @ResponseContentType, @ResponseHeaders, @ResponseStatusCode, @ResponseTimestamp, @User)", new { @Id = logApi.Id, @Application = logApi.Application, @Machine = logApi.Machine, @RequestContentBody = logApi.RequestContentBody, @RequestContentType = logApi.RequestContentType, @RequestHeaders = JsonConvert.SerializeObject(logApi.RequestHeaders), @RequestIpAddress = logApi.RequestIpAddress, @RequestMethod = logApi.RequestMethod, @RequestRouteData = logApi.RequestRouteData?.ToString(), @RequestRouteTemplate = logApi.RequestRouteTemplate, @RequestTimestamp = logApi.RequestTimestamp, @RequestUri = logApi.RequestUri, @ResponseContentBody = logApi.ResponseContentBody, @ResponseContentType = logApi.ResponseContentType, @ResponseHeaders = JsonConvert.SerializeObject(logApi.ResponseHeaders.ToList()), @ResponseStatusCode = logApi.ResponseStatusCode, @ResponseTimestamp = logApi.ResponseTimestamp, @User = logApi.User } ); } }
private LogApi ObterDadosInterceptadosDaRequisicaoDaApi(HttpRequestMessage request, string id) { //var context = ((HttpContextBase)request.Properties["MS_HttpContext"]); var context = ((OwinContext)request.Properties["MS_OwinContext"]); var routeData = request.GetRouteData(); var logApi = new LogApi(); //Obtenho nome do projeto logApi.Application = Assembly.GetExecutingAssembly().FullName; if (context.Request.User != null) { logApi.User = context.Request.User.Identity.Name; } logApi.Machine = Environment.MachineName; logApi.Id = id; if (context.Request != null) { logApi.RequestContentType = context.Request.ContentType; if (routeData != null) { logApi.RequestRouteTemplate = routeData.Route.RouteTemplate; logApi.RequestRouteData = routeData; } logApi.RequestIpAddress = context.Request.RemoteIpAddress; } if (request.Method != null) { logApi.RequestMethod = request.Method.Method; } if (request.Headers != null) { logApi.RequestHeaders = SetaDadosDoHeaderParaUmDicionario(request.Headers); } logApi.RequestTimestamp = DateTime.Now; if (request.RequestUri != null) { logApi.RequestUri = request.RequestUri.AbsoluteUri; } return(logApi); }