Пример #1
0
        public void Log(LogLevel logLevel, string messageWithPii, string messageScrubbed)
        {
            if (IsLoggingEnabled(logLevel))
            {
                string correlationId = _correlationId.Equals(Guid.Empty)
                    ? string.Empty
                    : " - " + _correlationId;

                var    msalIdParameters = MsalIdHelper.GetMsalIdParameters(this);
                string os = "N/A";
                if (msalIdParameters.TryGetValue(MsalIdParameter.OS, out string osValue))
                {
                    os = osValue;
                }

                bool messageWithPiiExists = !string.IsNullOrWhiteSpace(messageWithPii);
                // If we have a message with PII, and PII logging is enabled, use the PII message, else use the scrubbed message.
                bool   isLoggingPii = messageWithPiiExists && PiiLoggingEnabled;
                string messageToLog = isLoggingPii ? messageWithPii : messageScrubbed;

                string log = string.Format(CultureInfo.InvariantCulture, "{0} MSAL {1} {2} {3} [{4}{5}]{6} {7}",
                                           isLoggingPii ? "(True)" : "(False)",
                                           MsalIdHelper.GetMsalVersion(),
                                           msalIdParameters[MsalIdParameter.Product],
                                           os, DateTime.UtcNow, correlationId, ClientInformation, messageToLog);

                if (_isDefaultPlatformLoggingEnabled)
                {
                    switch (logLevel)
                    {
                    case LogLevel.Error:
                        _platformLogger.Error(log);
                        break;

                    case LogLevel.Warning:
                        _platformLogger.Warning(log);
                        break;

                    case LogLevel.Info:
                        _platformLogger.Information(log);
                        break;

                    case LogLevel.Verbose:
                        _platformLogger.Verbose(log);
                        break;
                    }
                }

                _loggingCallback.Invoke(logLevel, log, isLoggingPii);
            }
        }
Пример #2
0
        private void Log(LogLevel authLogLevel, string messageWithPii, string messageScrubbed)
        {
            if (_loggingCallback == null || authLogLevel >= _logLevel)
            {
                return;
            }

            bool messageWithPiiExists = !string.IsNullOrWhiteSpace(messageWithPii);

            // If we have a message with PII, and PII logging is enabled, use the PII message, else use the scrubbed message.
            bool   isLoggingPii = messageWithPiiExists && PiiLoggingEnabled;
            string messageToLog = isLoggingPii ? messageWithPii : messageScrubbed;

            string log = $"{isLoggingPii} AUTH {DateTime.UtcNow} {ClientInformation} {messageToLog}";

            if (_isDefaultPlatformLoggingEnabled)
            {
                switch (authLogLevel)
                {
                case LogLevel.Error:
                    _platformLogger.Error(log);
                    break;

                case LogLevel.Warning:
                    _platformLogger.Warning(log);
                    break;

                case LogLevel.Info:
                    _platformLogger.Information(log);
                    break;

                case LogLevel.Verbose:
                    _platformLogger.Verbose(log);
                    break;
                }
            }

            _loggingCallback.Invoke(authLogLevel, log, isLoggingPii);
        }
Пример #3
0
        public Func <CancellationToken, Task> ConstructAction(IStateMachine machineInstance, State state, string contentType, string payload, IDictionary <string, string> configuration)
        {
            return((cancellationToken) =>
            {
                string headersString = null;
                IDictionary <string, string> headers = null;
                if (configuration != null && configuration.TryGetValue("headers", out headersString))
                {
                    headers = StringSerializer.Deserialize <IDictionary <string, string> >(headersString);
                }

                var actionSettings = new ActionConfiguration(configuration, headers);

                if (payload == null)
                {
                    payload = actionSettings.MessageBody;
                }


                using (var connection = ConnectionFactory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        var properties = channel.CreateBasicProperties();

                        properties.ContentType = contentType ?? actionSettings.ContentType ?? "text/plain";
                        properties.Type = actionSettings.MessageType;
                        if (actionSettings.Headers != null)
                        {
                            properties.Headers = actionSettings.Headers
                                                 .ToDictionary <KeyValuePair <string, string>,
                                                                string, object>(kvp => kvp.Key, kvp => kvp.Value);
                        }

                        properties.Headers = properties.Headers ?? new Dictionary <string, object>();

                        properties.Headers.Add("MachineId", machineInstance.MachineId);

                        try
                        {
                            channel.BasicPublish(actionSettings.ExchangeName,
                                                 actionSettings.MessageType, properties, Encoding.UTF8.GetBytes(payload));

                            Logger
                            .ForContext("ActionConfiguration", actionSettings, true)
                            .Debug("Successfully published RabbitMq action message. " +
                                   "MessageType: {messageType}, Payload: {payload}",
                                   actionSettings.MessageType, payload);
                        }
                        catch (Exception ex)
                        {
                            ex.Data.Add("VirtualHost", ConnectionFactory.VirtualHost);
                            ex.Data.Add("HostName", ConnectionFactory.HostName);
                            ex.Data.Add("ExchangeName", actionSettings.ExchangeName);
                            ex.Data.Add("RoutingKey", properties.Type);
                            ex.Data.Add("Headers", properties.Headers);
                            ex.Data.Add("Payload", payload);

                            Logger.Error(ex, "Failed to publish action message to RabbitMq. " +
                                         "MessageType: {messageType}, Payload: {payload}",
                                         actionSettings.MessageType, payload);

                            throw;
                        }

                        return Task.CompletedTask;
                    }
            });
        }