<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using log4net;
using log4net.Appender;
using log4net.Core;
using Newtonsoft.Json;

namespace Log4Net.InitialStateAppender
{
    public class ApiAppender : AppenderSkeleton
    {
        public string ApiKey { get; set; }
        public string ApiRootUrl { get; set; }
        public Guid BucketId { get; set; }
        public string Ndc { get; set; }

        protected override void Append(LoggingEvent loggingEvent)
        {
            try
            {
                var thread = new Thread(ShipLogMessage);
                thread.IsBackground = true;
                thread.Start(loggingEvent);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void ShipLogMessage(object loggingEvent)
        {
            try
            {
                var typedLoggingEvent = (LoggingEvent) loggingEvent;
                string logMessage = RenderLoggingEvent(typedLoggingEvent);
                string url = string.Format("{0}{1}/{2}", ApiRootUrl, BucketId, ApiKey);

                Trace.WriteLine(url);
                Trace.WriteLine(logMessage);

                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method = "POST";

                //speed up the request by not autodetecting proxy http://go.microsoft.com/fwlink/?linkid=14202
                webRequest.Proxy = null;
                webRequest.ContentType = "application/json";

                string trackerId = null;

                if (logMessage.Contains("tid:"))
                {
                    trackerId = logMessage.Substring(logMessage.IndexOf("tid:") + 1, logMessage.IndexOf(":tid"));
                }
                
                string json = JsonConvert.SerializeObject(new LogMessageRequest
                                                          {
                                                              Log = logMessage,
                                                              DateTime = DateTime.UtcNow,
                                                              SignalSource = typedLoggingEvent.LoggerName,
                                                              TrackerId = trackerId
                                                          });
                byte[] contentBytes = Encoding.UTF8.GetBytes(json);
                webRequest.ContentLength = contentBytes.Length;
                Stream stream = webRequest.GetRequestStream();
                stream.Write(contentBytes, 0, contentBytes.Length);
                stream.Flush();
                stream.Close();

                using (webRequest.GetResponse())
                {
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private class LogMessageRequest
        {
            [JsonProperty(PropertyName = "log")]
            public string Log { get; set; }
            [JsonProperty(PropertyName = "date_time")]
            public DateTime DateTime { get; set; }<commit_msg>using the NDC property as the tracking id, in syslog format this is the message id as well<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Util;
using Newtonsoft.Json;

namespace Log4Net.InitialStateAppender
{
    public class ApiAppender : AppenderSkeleton
    {
        public string ApiKey { get; set; }
        public string ApiRootUrl { get; set; }
        public Guid BucketId { get; set; }
        public string Ndc { get; set; }

        protected override void Append(LoggingEvent loggingEvent)
        {
            try
            {
                var thread = new Thread(ShipLogMessage);
                thread.IsBackground = true;
                thread.Start(loggingEvent);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void ShipLogMessage(object loggingEvent)
        {
            try
            {
                var typedLoggingEvent = (LoggingEvent) loggingEvent;
                string logMessage = RenderLoggingEvent(typedLoggingEvent);
                string url = string.Format("{0}{1}/{2}", ApiRootUrl, BucketId, ApiKey);

                Trace.WriteLine(url);
                Trace.WriteLine(logMessage);

                WebRequest webRequest = WebRequest.Create(url);
                webRequest.Method = "POST";

                //speed up the request by not autodetecting proxy http://go.microsoft.com/fwlink/?linkid=14202
                webRequest.Proxy = null;
                webRequest.ContentType = "application/json";

                string trackerId = null;
                var ndc = typedLoggingEvent.LookupProperty("NDC") as ThreadContextStack;
                if (ndc != null && ndc.Count > 0)
                {
                    // the NDC represents a context stack, whose levels are separated by whitespace. we will use this as our MessageId.
                    trackerId = ndc.ToString();
                }

                string json = JsonConvert.SerializeObject(new LogMessageRequest
                                                          {
                                                              Log = logMessage,
                                                              DateTime = DateTime.UtcNow,
                                                              SignalSource = typedLoggingEvent.LoggerName,
                                                              TrackerId = trackerId
                                                          });
                byte[] contentBytes = Encoding.UTF8.GetBytes(json);
                webRequest.ContentLength = contentBytes.Length;
                Stream stream = webRequest.GetRequestStream();
                stream.Write(contentBytes, 0, contentBytes.Length);
                stream.Flush();
                stream.Close();

                using (webRequest.GetResponse())
                {
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private class LogMessageRequest
        {
            [JsonProperty(PropertyName = "log")]
            public string Log { get; set; }
            [JsonProperty(PropertyName = "date_time")]
            public DateTime DateTime { get; set; }
{'fraction_non_alphanumeric': 0.07626865671641792, 'fraction_numerical': 0.004626865671641791, 'mean_word_length': 1.2808032675289311, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1782171', 'n_tokens_mistral': 1555, 'n_tokens_neox': 1469, 'n_words': 360}