Esempio n. 1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            // We don't support any other type of state.
            IEnumerable <KeyValuePair <string, object> > properties = state as IEnumerable <KeyValuePair <string, object> >;
            string formattedMessage = formatter?.Invoke(state, exception);

            if (string.IsNullOrEmpty(formattedMessage) || properties == null || !IsEnabled(logLevel) || IsFromTraceWriter(properties))
            {
                return;
            }

            IDictionary <string, object> scopeProperties = DictionaryLoggerScope.GetMergedStateDictionary();

            if (!scopeProperties.TryGetValue(ScriptConstants.LoggerFunctionNameKey, out string functionName))
            {
                // We have nowhere to write the file if we don't know the function name
                return;
            }

            // this function name starts with "Functions.", but file paths do not include this
            string functionsPrefix = "Functions.";

            if (functionName.StartsWith(functionsPrefix))
            {
                functionName = functionName.Substring(functionsPrefix.Length);
            }

            TraceWriter traceWriter = _writerCache.GetOrAdd(functionName, (n) => CreateFileTraceWriter(n, _config));
            TraceLevel  traceLevel  = GetTraceLevel(logLevel);
            TraceEvent  traceEvent  = new TraceEvent(traceLevel, formattedMessage, _categoryName, exception);

            traceWriter.Trace(traceEvent);
        }
        private static string GetFunctionName()
        {
            IDictionary <string, object> scopeProperties = DictionaryLoggerScope.GetMergedStateDictionary();

            if (!scopeProperties.TryGetValue(ScriptConstants.LoggerFunctionNameKey, out string functionName))
            {
                return(null);
            }

            // this function name starts with "Functions.", but file paths do not include this
            string functionsPrefix = "Functions.";

            if (functionName.StartsWith(functionsPrefix))
            {
                functionName = functionName.Substring(functionsPrefix.Length);
            }

            return(functionName);
        }
        public static IDisposable Push(object state)
        {
            IDictionary <string, object> stateValues;

            if (state is IEnumerable <KeyValuePair <string, object> > stateEnum)
            {
                // Convert this to a dictionary as we have scenarios where we cannot have duplicates. In this
                // case, if there are dupes, the later entry wins.
                stateValues = new Dictionary <string, object>();
                foreach (var entry in stateEnum)
                {
                    stateValues[entry.Key] = entry.Value;
                }
            }
            else
            {
                // There's nothing we can do with other states.
                return(null);
            }

            Current = new DictionaryLoggerScope(new ReadOnlyDictionary <string, object>(stateValues), Current);
            return(new DisposableScope());
        }
Esempio n. 4
0
 public IDisposable BeginScope <TState>(TState state) => DictionaryLoggerScope.Push(state);
 private DictionaryLoggerScope(IReadOnlyDictionary <string, object> state, DictionaryLoggerScope parent)
 {
     State  = state;
     Parent = parent;
 }
Esempio n. 6
0
 private DictionaryLoggerScope(object state, DictionaryLoggerScope parent)
 {
     _state = state;
     Parent = parent;
 }