Exemplo n.º 1
0
        void ILogger.Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            var isEnabled = (this as ILogger).IsEnabled(logLevel);

            if (!isEnabled)
            {
                return;
            }

            var entry = new RuntimeLogEntry
            {
                Category  = this.Category,
                Level     = logLevel,
                Message   = state.ToString(),
                Exception = exception,
                EventId   = eventId.Id,
                State     = state,
            };

            if (state is string)
            {
                entry.StateText = state.ToString();
            }
            else if (state is IEnumerable <KeyValuePair <string, object> > properties)
            {
                entry.StateProperties = new Dictionary <string, object>();
                foreach (var item in properties)
                {
                    entry.StateProperties[item.Key] = item.Value;
                }
            }

            if (this.Provider.ScopeProvider != null)
            {
                this.Provider.ScopeProvider.ForEachScope((obj, loggingProps) =>
                {
                    if (entry.Scopes == null)
                    {
                        entry.Scopes = new List <RuntimeLogScope>();
                    }

                    var scope = new RuntimeLogScope();
                    entry.Scopes.Add(scope);

                    if (obj is string)
                    {
                        scope.Text = obj.ToString();
                    }
                    else if (obj is IEnumerable <KeyValuePair <string, object> > properties)
                    {
                        if (scope.Properties == null)
                        {
                            scope.Properties = new Dictionary <string, object>();
                        }

                        foreach (var item in properties)
                        {
                            scope.Properties[item.Key] = item.Value;
                        }
                    }
                }, state);
            }

            this.Provider.WriteLog(entry, this.Resource, this.DetectorId);
        }
Exemplo n.º 2
0
        public void WriteLog(RuntimeLogEntry info, IResource resource, string detectorId)
        {
            if (info == null || info.Message == null)
            {
                return;
            }

            var reqid = info.Category;

            _runtimeLogs.AddOrUpdate(reqid,
                                     id => new List <RuntimeLogEntry> {
                info
            },                                     // Adding
                                     (id, logs) => // Updating
            {
                logs.Add(info);
                return(logs);
            });

            string message;

            if (info.Exception != null)
            {
                message = info.Message + ": " + info.Exception.Message;
            }
            else
            {
                message = info.Message ?? "";
            }

            switch (info.Level)
            {
            case LogLevel.Critical:
            case LogLevel.Error:
                DiagnosticsETWProvider.Instance.LogRuntimeLogError(
                    reqid,
                    detectorId ?? "",
                    resource?.SubscriptionId ?? "",
                    resource?.ResourceGroup ?? "",
                    resource?.Name ?? "",
                    info.Exception?.GetType()?.ToString() ?? "",
                    info.Exception?.ToString() ?? "",
                    message);
                break;

            case LogLevel.Warning:
                DiagnosticsETWProvider.Instance.LogRuntimeLogWarning(
                    reqid,
                    detectorId ?? "",
                    resource?.SubscriptionId ?? "",
                    resource?.ResourceGroup ?? "",
                    resource?.Name ?? "",
                    info.Exception?.GetType()?.ToString() ?? "",
                    info.Exception?.ToString() ?? "",
                    message);
                break;

            case LogLevel.Information:
                DiagnosticsETWProvider.Instance.LogRuntimeLogInformation(
                    reqid,
                    detectorId ?? "",
                    resource?.SubscriptionId ?? "",
                    resource?.ResourceGroup ?? "",
                    resource?.Name ?? "",
                    info.Exception?.GetType()?.ToString() ?? "",
                    info.Exception?.ToString() ?? "",
                    message);
                break;

            case LogLevel.Trace:
            case LogLevel.Debug:
                // No trace/debug messages are emitted.
                break;
            }
        }