<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogJamExceptionLogger.cs">
// Copyright (c) 2011-2014 logjam.codeplex.com.  
// </copyright>
// Licensed under the <a href="http://logjam.codeplex.com/license">Apache License, Version 2.0</a>;
// you may not use this file except in compliance with the License.
// --------------------------------------------------------------------------------------------------------------------

namespace LogJam.WebApi
{
	using System;
	using System.Diagnostics.Contracts;
	using System.Net.Http;
	using System.Threading;
	using System.Threading.Tasks;
	using System.Web.Http.ExceptionHandling;

	using LogJam.Trace;
	using LogJam.WebApi.Tasks;


	/// <summary>
	/// Logs Web API exceptions to a LogJam <see cref="Tracer"/>.
	/// </summary>
	public sealed class LogJamExceptionLogger : IExceptionLogger
	{

		private readonly Tracer _tracer;

		/// <summary>
		/// Creates a new <see cref="LogJamExceptionLogger"/>, which logs Web API exceptions to a <see cref="Tracer"/> obtained from
		/// <paramref name="tracerFactory"/>.
		/// </summary>
		/// <param name="tracerFactory">A <see cref="ITracerFactory"/>.</param>
		public LogJamExceptionLogger(ITracerFactory tracerFactory)
		{
			Contract.Requires<ArgumentNullException>(tracerFactory != null);

			_tracer = tracerFactory.TracerFor(this);
		}

		public Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken)
		{
			if (context.CatchBlock.IsTopLevel)
			{
				// Only log top-level exceptions
				var request = context.Request;
				var exception = context.Exception;

				if (request.HasRequestExceptionBeenLogged(exception))
				{
					// Don't repeatedly log the same exception
					_tracer.Error("Exception thrown handling Web API request: {0} {1}\n    (exception already logged)", request.Method,
						request.RequestUri.OriginalString);
				}
				else
				{
					// exception hasn't previously been logged
					_tracer.Error(exception, "Exception thrown handling Web API request: {0} {1}", request.Method,
						request.RequestUri.OriginalString);
					request.LoggedRequestException(exception);
				}
			}

			return TaskHelper.Completed;
		}

	}

}<commit_msg>#9 - Web API ExceptionLogger should not filter exceptions based on whether the calling HttpHandler is top-level or not.<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogJamExceptionLogger.cs">
// Copyright (c) 2011-2014 logjam.codeplex.com.  
// </copyright>
// Licensed under the <a href="http://logjam.codeplex.com/license">Apache License, Version 2.0</a>;
// you may not use this file except in compliance with the License.
// --------------------------------------------------------------------------------------------------------------------

namespace LogJam.WebApi
{
	using System;
	using System.Diagnostics.Contracts;
	using System.Net.Http;
	using System.Threading;
	using System.Threading.Tasks;
	using System.Web.Http.ExceptionHandling;

	using LogJam.Trace;
	using LogJam.WebApi.Tasks;


	/// <summary>
	/// Logs Web API exceptions to a LogJam <see cref="Tracer"/>.
	/// </summary>
	public sealed class LogJamExceptionLogger : IExceptionLogger
	{

		private readonly Tracer _tracer;

		/// <summary>
		/// Creates a new <see cref="LogJamExceptionLogger"/>, which logs Web API exceptions to a <see cref="Tracer"/> obtained from
		/// <paramref name="tracerFactory"/>.
		/// </summary>
		/// <param name="tracerFactory">A <see cref="ITracerFactory"/>.</param>
		public LogJamExceptionLogger(ITracerFactory tracerFactory)
		{
			Contract.Requires<ArgumentNullException>(tracerFactory != null);

			_tracer = tracerFactory.TracerFor(this);
		}

		public Task LogAsync(ExceptionLoggerContext context, CancellationToken cancellationToken)
		{
			var request = context.Request;
			var exception = context.Exception;

			if (request.HasRequestExceptionBeenLogged(exception))
			{
				// Don't repeatedly log the same exception
				_tracer.Error("Exception thrown handling Web API request: {0} {1}\n    (exception already logged)", request.Method,
					request.RequestUri.OriginalString);
			}
			else
			{
				// exception hasn't previously been logged
				_tracer.Error(exception, "Exception thrown handling Web API request: {0} {1}", request.Method,
					request.RequestUri.OriginalString);
				request.LoggedRequestException(exception);
			}

			return TaskHelper.Completed;
		}

	}

}
{'fraction_non_alphanumeric': 0.20831519373095342, 'fraction_numerical': 0.006312581628210709, 'mean_word_length': 2.4653092006033184, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '7238132', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1197, 'n_words': 323}