<commit_before>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OfficeDevPnP.Core.Utilities
{

    public sealed class PnPMonitoredScope : IDisposable
    {
        private Stopwatch _stopWatch;
        private string _name;
        internal string LocalMachine = Environment.MachineName;
        private Guid _correlationId;

        public PnPMonitoredScope(string name)
        {

            _stopWatch = new Stopwatch();
            _name = name;
            _stopWatch.Start();
            _correlationId = Guid.NewGuid();

            LogInfo(CoreResources.PnPMonitoredScope_Code_execution_started);
            Indent();
        }

        public void Indent()
        {
            Trace.Indent();
        }

        /// <summary>
        /// Decreases the current IndentLevel by one.
        /// </summary>
        public void Unindent()
        {
            Trace.Unindent();
        }

        public Guid CorrelationId
        {
            get { return _correlationId; }
        }

        public void LogError(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceError(log);
            WriteLogToConsole(log);
        }

        public void LogInfo(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceInformation(log);
            WriteLogToConsole(log);
        }

        public void LogWarning(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceWarning(log);
            WriteLogToConsole(log);
        }
        private string GetLogEntry(string source, string message, params object[] args)
        {
            try
            {
                string msg = string.Empty;

                if (args == null || args.Length == 0)
                {
                    msg = message.Replace("{", "{{").Replace("}", "}}");
                }
                else
                {
                    msg = String.Format(CultureInfo.CurrentCulture, message, args);
                }

                string log = string.Format(CultureInfo.CurrentCulture, "{0} [{1}] {2} {3}ms {4}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), source, msg, _stopWatch.ElapsedMilliseconds, this.CorrelationId);
                return log;
            }
            catch (Exception e)
            {
                return string.Format("Error while generating log information, {0}", e);
            }
        }


        public void Dispose()
        {
            _stopWatch.Stop();
            Unindent();
            LogInfo(CoreResources.PnPMonitoredScope_Code_execution_ended, _stopWatch.ElapsedMilliseconds);
            Trace.Flush();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Console.WriteLine(System.String,System.Object,System.Object)")]<commit_msg>Created overloaded constructor to allow for creating monitored scope without name. Name then defaults to calling method<commit_after>﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OfficeDevPnP.Core.Utilities
{

    public sealed class PnPMonitoredScope : IDisposable
    {
        private Stopwatch _stopWatch;
        private string _name;
        internal string LocalMachine = Environment.MachineName;
        private Guid _correlationId;

        public PnPMonitoredScope()
        {
            StackFrame frame = new StackFrame(1);
            var method = frame.GetMethod();
            var name = method.Name;
            StartScope(name);
        }

        public PnPMonitoredScope(string name)
        {
            StartScope(name);
        }

        private void StartScope(string name)
        {
            _stopWatch = new Stopwatch();
            _name = name;
            _stopWatch.Start();
            _correlationId = Guid.NewGuid();

            LogInfo(CoreResources.PnPMonitoredScope_Code_execution_started);
            Indent();
        }

        private void EndScope()
        {
            _stopWatch.Stop();
            Unindent();
            LogInfo(CoreResources.PnPMonitoredScope_Code_execution_ended, _stopWatch.ElapsedMilliseconds);
            Trace.Flush();
        }

        public void Indent()
        {
            Trace.Indent();
        }

        /// <summary>
        /// Decreases the current IndentLevel by one.
        /// </summary>
        public void Unindent()
        {
            Trace.Unindent();
        }

        public Guid CorrelationId
        {
            get { return _correlationId; }
        }

        public void LogError(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceError(log);
            WriteLogToConsole(log);
        }

        public void LogInfo(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceInformation(log);
            WriteLogToConsole(log);
        }

        public void LogWarning(string message, params object[] args)
        {
            var log = GetLogEntry(_name, message, args);
            Trace.TraceWarning(log);
            WriteLogToConsole(log);
        }
        private string GetLogEntry(string source, string message, params object[] args)
        {
            try
            {
                string msg = string.Empty;

                if (args == null || args.Length == 0)
                {
                    msg = message.Replace("{", "{{").Replace("}", "}}");
                }
                else
                {
                    msg = String.Format(CultureInfo.CurrentCulture, message, args);
                }

                string log = string.Format(CultureInfo.CurrentCulture, "{0} [{1}] {2} {3}ms {4}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), source, msg, _stopWatch.ElapsedMilliseconds, this.CorrelationId);
                return log;
            }
            catch (Exception e)
            {
                return string.Format("Error while generating log information, {0}", e);
            }
        }


        public void Dispose()
        {
            EndScope();
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "System.Console.WriteLine(System.String,System.Object,System.Object)")]
{'fraction_non_alphanumeric': 0.09621746008917015, 'fraction_numerical': 0.003307924636847404, 'mean_word_length': 1.8119692680954307, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2763281', 'n_tokens_mistral': 1818, 'n_tokens_neox': 1730, 'n_words': 411}