<commit_before>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Framework.Logging;
using System;
using Serilog.Core;
using Serilog.Events;
using FrameworkLogger = Microsoft.Framework.Logging.ILogger;
using System.Reflection;

namespace Serilog.Framework.Logging
{
    public class SerilogLogger : FrameworkLogger
    {
        readonly SerilogLoggerProvider _provider;
        readonly string _name;
        readonly ILogger _logger;

        public SerilogLogger(
            SerilogLoggerProvider provider,
            ILogger logger = null,
            string name = null)
        {
            if (provider == null) throw new ArgumentNullException("provider");
            _provider = provider;
            _name = name;
            _logger = logger;

            if (_logger == null)
            {
                // If a logger was passed, the provider has already added itself as an enricher
                _logger = Serilog.Log.Logger.ForContext(new[] { provider });
            }

            if (_name != null)
            {
                _logger = _logger.ForContext(Constants.SourceContextPropertyName, name);
            }
        }

        // On CTP
        public IDisposable BeginScope(object state)
        {
            return BeginScopeImpl(state);
        }

        // Dev
        IDisposable BeginScopeImpl(object state)
        {
            return _provider.BeginScopeImpl(_name, state);
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return _logger.IsEnabled(ConvertLevel(logLevel));
        }

        public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
        {
            var level = ConvertLevel(logLevel);
            if (!_logger.IsEnabled(level))
            {
                return;
            }

            var logger = _logger;
            string messageTemplate = null;

            var structure = state as ILogValues;
            if (structure != null)
            {
                foreach (var property in structure.GetValues())
                {
                    if (property.Key == "{OriginalFormat}" && property.Value is string)
                    {
                        messageTemplate = (string)property.Value;
                    }

                    logger = logger.ForContext(property.Key, property.Value);
                }

                var stateType = state.GetType();
                var stateTypeInfo = stateType.GetTypeInfo();
                // Imperfect, but at least eliminates `1 and + names
                if (messageTemplate == null && !stateTypeInfo.IsGenericType && !stateTypeInfo.IsNested)
                {
                    messageTemplate = "{" + stateType.Name + ":l}";
                    logger = logger.ForContext(stateType.Name, LogFormatter.Formatter(state, null));
                }
            }

            if (messageTemplate == null && state != null)
            {
                messageTemplate = LogFormatter.Formatter(state, null);
            }

            if (string.IsNullOrEmpty(messageTemplate))
            {
                return;
            }

            if (eventId != 0)
            {
                logger = logger.ForContext("EventId", eventId, false);
            }

            logger.Write(level, exception, messageTemplate);
        }
<commit_msg>Avoid passing through the {OriginalFormat} property<commit_after>// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Framework.Logging;
using System;
using Serilog.Core;
using Serilog.Events;
using FrameworkLogger = Microsoft.Framework.Logging.ILogger;
using System.Reflection;

namespace Serilog.Framework.Logging
{
    public class SerilogLogger : FrameworkLogger
    {
        readonly SerilogLoggerProvider _provider;
        readonly string _name;
        readonly ILogger _logger;

        public SerilogLogger(
            SerilogLoggerProvider provider,
            ILogger logger = null,
            string name = null)
        {
            if (provider == null) throw new ArgumentNullException("provider");
            _provider = provider;
            _name = name;
            _logger = logger;

            if (_logger == null)
            {
                // If a logger was passed, the provider has already added itself as an enricher
                _logger = Serilog.Log.Logger.ForContext(new[] { provider });
            }

            if (_name != null)
            {
                _logger = _logger.ForContext(Constants.SourceContextPropertyName, name);
            }
        }

        // On CTP
        public IDisposable BeginScope(object state)
        {
            return BeginScopeImpl(state);
        }

        // Dev
        IDisposable BeginScopeImpl(object state)
        {
            return _provider.BeginScopeImpl(_name, state);
        }

        public bool IsEnabled(LogLevel logLevel)
        {
            return _logger.IsEnabled(ConvertLevel(logLevel));
        }

        public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
        {
            var level = ConvertLevel(logLevel);
            if (!_logger.IsEnabled(level))
            {
                return;
            }

            var logger = _logger;
            string messageTemplate = null;

            var structure = state as ILogValues;
            if (structure != null)
            {
                foreach (var property in structure.GetValues())
                {
                    if (property.Key == "{OriginalFormat}" && property.Value is string)
                    {
                        messageTemplate = (string)property.Value;
                    }
                    else
                    {
                        logger = logger.ForContext(property.Key, property.Value);
                    }
                }

                var stateType = state.GetType();
                var stateTypeInfo = stateType.GetTypeInfo();
                // Imperfect, but at least eliminates `1 and + names
                if (messageTemplate == null && !stateTypeInfo.IsGenericType && !stateTypeInfo.IsNested)
                {
                    messageTemplate = "{" + stateType.Name + ":l}";
                    logger = logger.ForContext(stateType.Name, LogFormatter.Formatter(state, null));
                }
            }

            if (messageTemplate == null && state != null)
            {
                messageTemplate = LogFormatter.Formatter(state, null);
            }

            if (string.IsNullOrEmpty(messageTemplate))
            {
                return;
            }

            if (eventId != 0)
            {
                logger = logger.ForContext("EventId", eventId, false);
            }

            logger.Write(level, exception, messageTemplate);
        }

{'fraction_non_alphanumeric': 0.08284354883851106, 'fraction_numerical': 0.001119507416736636, 'mean_word_length': 1.5452279202279202, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '6605515', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1658, 'n_words': 442}