Example #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);
        }