コード例 #1
0
        private CloudQueue GetQueue(LogEventLevel level, IReadOnlyDictionary <string, LogEventPropertyValue> properties)
        {
            if (!_separateQueuesByLogLevel)
            {
                return(_queue);
            }

            var logLevelSuffix = GetLogLevelSuffix(level, properties);

            if (_queuesDictionary.TryGetValue(logLevelSuffix, out var queue))
            {
                return(queue);
            }

            var queueName = $"{_storageQueueName}-{logLevelSuffix}";

            queue = _cloudQueueProvider.GetCloudQueue(
                _storageAccount,
                queueName,
                _bypassQueueCreationValidation);

            _queuesDictionary.TryAdd(logLevelSuffix, queue);

            return(queue);
        }
        /// <summary>
        /// Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            var queue = _cloudQueueProvider.GetCloudQueue(_storageAccount, _storageQueueName, _bypassQueueCreationValidation);

            CloudQueueClient  queueClient      = _storageAccount.CreateCloudQueueClient();
            CloudQueue        storageQueueName = queueClient.GetQueueReference(_storageQueueName);
            CloudQueueMessage message          = new CloudQueueMessage(JsonConvert.SerializeObject(logEvent));

            queue.AddMessageAsync(message)
            .SyncContextSafeWait(_waitTimeoutMilliseconds);
        }
        /// <summary>
        /// Emit the provided log event to the sink.
        /// </summary>
        /// <param name="logEvent">The log event to write.</param>
        public void Emit(LogEvent logEvent)
        {
            TextWriter writer = new StringWriter();

            _textFormatter.Format(logEvent, writer);
            var output = writer.ToString();

            var queue = _cloudQueueProvider.GetCloudQueue(_storageAccount, _storageQueueName, _bypassQueueCreationValidation);

            CloudQueueClient  queueClient      = _storageAccount.CreateCloudQueueClient();
            CloudQueue        storageQueueName = queueClient.GetQueueReference(_storageQueueName);
            CloudQueueMessage message          = new CloudQueueMessage(output);

            queue.AddMessageAsync(message)
            .SyncContextSafeWait(_waitTimeoutMilliseconds);
        }
コード例 #4
0
 /// <summary>
 /// Construct a sink that saves logs to the specified storage account.
 /// </summary>
 /// <param name="storageAccount">The Cloud Storage Account containing the queue.</param>
 /// <param name="textFormatter"></param>
 /// <param name="storageQueueName">Queue name that messages will be written to.</param>
 /// <param name="bypassQueueCreationValidation">Bypass the exception in case the queue creation fails.</param>
 /// <param name="separateQueuesByLogLevel">Flag for several queues usage by log level</param>
 /// <param name="cloudQueueProvider">Cloud queue provider to get current queue.</param>
 public AzureQueueStorageSink(
     CloudStorageAccount storageAccount,
     ITextFormatter textFormatter,
     string storageQueueName                = null,
     bool bypassQueueCreationValidation     = false,
     bool separateQueuesByLogLevel          = false,
     ICloudQueueProvider cloudQueueProvider = null)
 {
     _textFormatter    = textFormatter;
     _storageAccount   = storageAccount;
     _storageQueueName = storageQueueName;
     _bypassQueueCreationValidation = bypassQueueCreationValidation;
     _separateQueuesByLogLevel      = separateQueuesByLogLevel;
     _cloudQueueProvider            = cloudQueueProvider ?? new DefaultCloudQueueProvider();
     if (separateQueuesByLogLevel)
     {
         _queuesDictionary = new ConcurrentDictionary <string, CloudQueue>();
     }
     else
     {
         _queue = _cloudQueueProvider.GetCloudQueue(_storageAccount, _storageQueueName, _bypassQueueCreationValidation);
     }
 }