<commit_before>﻿using N2.Edit.Collaboration;
using N2.Plugin;
using N2.Security;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace N2.Edit.Collaboration
{
	[MessageSource]
	public class TraceReaderMessageProvider : MessageSourceBase, IAutoStart
	{
		private Queue<CollaborationMessage> messages = new Queue<CollaborationMessage>();
		private TraceEventType reportingLevel;
		private bool enabled;

		public TraceReaderMessageProvider(N2.Configuration.ConfigurationManagerWrapper config, ConnectionMonitor monitor)
		{
			monitor.Online += monitor_Online;
			reportingLevel = config.Sections.Management.Collaboration.ErrorReportingLevel;
			enabled = config.Sections.Management.Collaboration.ErrorReportingEnabled;
		}

		void monitor_Online(object sender, EventArgs e)
		{
			Trace.Listeners.Add(new Listener(this));
		}

		public override IEnumerable<CollaborationMessage> GetMessages(N2.Edit.Collaboration.CollaborationContext context)
		{
			return messages.Reverse().ToList();
		}

		public void Start()
		{
		}

		public void Stop()
		{
			Trace.Listeners.Remove("TraceReaderMessageProviderListener");
		}

		internal void Add(CollaborationMessage message)
		{
			messages.Enqueue(message);
			if (messages.Count > 10)
				messages.Dequeue();
		}

		class Listener : TraceListener
		{
			private TraceReaderMessageProvider messageProvider;

			public Listener(TraceReaderMessageProvider messageProvider)
			{
				Name = "TraceReaderMessageProviderListener";
				this.messageProvider = messageProvider;
			}

			public override void Write(string message)
			{
			}

			public override void WriteLine(string message)
			{
			}

			public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
			{
				if (messageProvider.enabled && eventType <= messageProvider.reportingLevel)
				{
					var titleBody = Split(string.Format(format, args));
					messageProvider.Add(new CollaborationMessage { Title = titleBody[0], Text = titleBody[1], Alert = false, Updated = eventCache.DateTime, RequiredPermission = Permission.Administer });
				}
				base.TraceEvent(eventCache, source, eventType, id, format, args);
			}

			public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
			{
				if (messageProvider.enabled && eventType <= messageProvider.reportingLevel)
				{
					var titleBody = Split(message);
					messageProvider.Add(new CollaborationMessage { Title = titleBody[0], Text = titleBody[1], Alert = false, Updated = eventCache.DateTime, RequiredPermission = Permission.Administer });
				}
				base.TraceEvent(eventCache, source, eventType, id, message);
			}

			private string[] Split(string message)
			{
				var i = message.IndexOfAny(Environment.NewLine.ToCharArray());
				if (i > 0)
					return new string[] { HttpUtility.HtmlEncode(message.Substring(0, i)), FormatErrorBody(message.Substring(i).Trim()) };
				else
					return new string[] { HttpUtility.HtmlEncode(message), "" };
			}


			private static string FormatErrorBody(string body)
			{
				return "<pre><code>" + HttpUtility.HtmlEncode(body) + "</code></pre>";
			}
		}
	}
}<commit_msg>Shorten error messages in admin<commit_after>﻿using N2.Edit.Collaboration;
using N2.Plugin;
using N2.Security;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace N2.Edit.Collaboration
{
	[MessageSource]
	public class TraceReaderMessageProvider : MessageSourceBase, IAutoStart
	{
		private Queue<CollaborationMessage> messages = new Queue<CollaborationMessage>();
		private TraceEventType reportingLevel;
		private bool enabled;

		public TraceReaderMessageProvider(N2.Configuration.ConfigurationManagerWrapper config, ConnectionMonitor monitor)
		{
			monitor.Online += monitor_Online;
			reportingLevel = config.Sections.Management.Collaboration.ErrorReportingLevel;
			enabled = config.Sections.Management.Collaboration.ErrorReportingEnabled;
		}

		void monitor_Online(object sender, EventArgs e)
		{
			Trace.Listeners.Add(new Listener(this));
		}

		public override IEnumerable<CollaborationMessage> GetMessages(N2.Edit.Collaboration.CollaborationContext context)
		{
			return messages.Reverse().ToList();
		}

		public void Start()
		{
		}

		public void Stop()
		{
			Trace.Listeners.Remove("TraceReaderMessageProviderListener");
		}

		internal void Add(CollaborationMessage message)
		{
			messages.Enqueue(message);
			if (messages.Count > 10)
				messages.Dequeue();
		}

		class Listener : TraceListener
		{
			private TraceReaderMessageProvider messageProvider;

			public Listener(TraceReaderMessageProvider messageProvider)
			{
				Name = "TraceReaderMessageProviderListener";
				this.messageProvider = messageProvider;
			}

			public override void Write(string message)
			{
			}

			public override void WriteLine(string message)
			{
			}

			public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string format, params object[] args)
			{
				if (messageProvider.enabled && eventType <= messageProvider.reportingLevel)
				{
					var titleBody = Split(string.Format(format, args));
					messageProvider.Add(new CollaborationMessage { Title = titleBody[0], Text = titleBody[1], Alert = false, Updated = eventCache.DateTime, RequiredPermission = Permission.Administer });
				}
				base.TraceEvent(eventCache, source, eventType, id, format, args);
			}

			public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message)
			{
				if (messageProvider.enabled && eventType <= messageProvider.reportingLevel)
				{
					var titleBody = Split(message);
					RemoveErrorHandlerTitle(titleBody);
					messageProvider.Add(new CollaborationMessage { Title = titleBody[0], Text = titleBody[1], Alert = false, Updated = eventCache.DateTime, RequiredPermission = Permission.Administer });
				}
				base.TraceEvent(eventCache, source, eventType, id, message);
			}

			private void RemoveErrorHandlerTitle(string[] titleBody)
			{
				var i = titleBody[0].IndexOf("ErrorHandler.Notify: Url: ");
				if (i >= 0)
					titleBody[0] = "Error: " + titleBody[0].Substring(i + "ErrorHandler.Notify: Url: ".Length);
			}

			private string[] Split(string message)
			{
				var i = message.IndexOfAny(Environment.NewLine.ToCharArray());
				if (i > 0)
					return new string[] { HttpUtility.HtmlEncode(message.Substring(0, i)), FormatErrorBody(message.Substring(i).Trim()) };
				else
					return new string[] { HttpUtility.HtmlEncode(message), "" };
			}


			private static string FormatErrorBody(string body)
			{
				return "<pre><code>" + HttpUtility.HtmlEncode(body) + "</code></pre>";
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.10251563181619892, 'fraction_numerical': 0.004653191798749455, 'mean_word_length': 4.542304593070105, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, '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': '138370', 'n_tokens_mistral': 2307, 'n_tokens_neox': 1915, 'n_words': 438}