<commit_before>﻿using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web.Http.Tracing;
using Bit.Core.Contracts;
using Microsoft.Owin;

namespace Bit.WebApi.Implementations
{
    public class DefaultWebApiTraceWritter : ITraceWriter
    {
        public virtual void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
        {
            if (request != null && (level == TraceLevel.Fatal || level == TraceLevel.Warn || level == TraceLevel.Error))
            {
                TraceRecord traceRecord = new TraceRecord(request, category, level);
                traceAction(traceRecord);

                IDependencyResolver scopeDependencyResolver = request.GetOwinContext().GetDependencyResolver();

                ILogger logger = scopeDependencyResolver.Resolve<ILogger>();
                IScopeStatusManager scopeStatusManager = scopeDependencyResolver.Resolve<IScopeStatusManager>();

                if (scopeStatusManager.WasSucceeded())
                    scopeStatusManager.MarkAsFailed();

                if (traceRecord.Exception != null)
                {
                    Exception exception = traceRecord.Exception;

                    if (exception is TargetInvocationException && exception.InnerException != null)
                        exception = exception.InnerException;
                    if (!logger.LogData.Any(d => d.Key == "X-CorrelationId"))
                        logger.AddLogData("X-CorrelationId", request.GetCorrelationId());
                    logger.AddLogData("WebExceptionType", exception.GetType().FullName);
                    logger.AddLogData("WebException", exception);
                    logger.AddLogData("WebApiErrorMessage", traceRecord.Message);<commit_msg>Don't log traces of fake web api requests >> swagger<commit_after>﻿using System;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Web.Http.Tracing;
using Bit.Core.Contracts;
using Microsoft.Owin;

namespace Bit.WebApi.Implementations
{
    public class DefaultWebApiTraceWritter : ITraceWriter
    {
        public virtual void Trace(HttpRequestMessage request, string category, TraceLevel level, Action<TraceRecord> traceAction)
        {
            if (request != null && request.GetOwinContext() != null && (level == TraceLevel.Fatal || level == TraceLevel.Warn || level == TraceLevel.Error))
            {
                TraceRecord traceRecord = new TraceRecord(request, category, level);
                traceAction(traceRecord);

                IDependencyResolver scopeDependencyResolver = request.GetOwinContext().GetDependencyResolver();

                ILogger logger = scopeDependencyResolver.Resolve<ILogger>();
                IScopeStatusManager scopeStatusManager = scopeDependencyResolver.Resolve<IScopeStatusManager>();

                if (scopeStatusManager.WasSucceeded())
                    scopeStatusManager.MarkAsFailed();

                if (traceRecord.Exception != null)
                {
                    Exception exception = traceRecord.Exception;

                    if (exception is TargetInvocationException && exception.InnerException != null)
                        exception = exception.InnerException;
                    if (!logger.LogData.Any(d => d.Key == "X-CorrelationId"))
                        logger.AddLogData("X-CorrelationId", request.GetCorrelationId());
                    logger.AddLogData("WebExceptionType", exception.GetType().FullName);
                    logger.AddLogData("WebException", exception);
                    logger.AddLogData("WebApiErrorMessage", traceRecord.Message);
{'fraction_non_alphanumeric': 0.09304841763592102, 'fraction_numerical': 0.0, 'mean_word_length': 2.5319961795606494, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6720657', 'n_tokens_mistral': 881, 'n_tokens_neox': 845, 'n_words': 179}