Я получаю сообщение «Указанное условие позиции добавления не выполнено» в компоненте Logger, который я реализовал с помощью CloudAppendBlob.AppendTextAsync. Я искал и видел, что могут возникнуть проблемы, если вы используете AppendTextAsync из нескольких задач, но я не делаю этого в своем коде. Я использую ConcurrentQueue для сообщений, которые должны регистрироваться, и это отдельная задача, считывающая из очереди и вызывающая AppendTextAsync.

Это мой код:

private async Task MessageLogger()
{
    string messages = "";
    while (true)
    {
        // Check if a new log file should be created
        if (DateTime.UtcNow.Day != _logCreatedUtc.Day)
        {
            // Create a new log file
            await CreateNewLogAsync();
        }

        if (_messageQueue.Count == 0 && messages.Length == 0)
        {
            // No messages to write, wait
            await Task.Delay(50);
            continue;
        }

        int n = 0;
        while ((this._messageQueue.Count > 0) && (n < 50))
        {
            string message;
            if (this._messageQueue.TryDequeue(out message))
            {
                messages += message;
                n++;
            }
        }

        try
        {
            // Append messages to Azure Blob
            await _appendBlob.AppendTextAsync(messages);
            messages = "";
        }
        catch (Microsoft.WindowsAzure.Storage.StorageException se)
            when(se.RequestInformation.HttpStatusCode == 404)
        {
            LogException("StorageException from CloudAppendBlob.AppendTextAsync", se);
            // Log file was deleted. Create a new log file
            await CreateNewLogAsync();
        }
        catch (AggregateException ex)
        {
            LogException("AggregateException from CloudAppendBlob.AppendTextAsync", ex);
            StorageException se = ExtractStorageException(ex);
            if (se != null && se.RequestInformation.ExtendedErrorInformation != null)
            {
                if (se.RequestInformation.ExtendedErrorInformation.ErrorCode == "BlockCountExceedsLimit")
                    // Block count exceeds limit. Create a new log file
                    await CreateNewLogAsync();
            }
        }
        catch (Exception ex)
        {
            LogException("Exception from CloudAppendBlob.AppendTextAsync", ex);
            await Task.Delay(1000);
        }
    }
}

И вот образец созданного файла журнала:

19.04.2018 00:00:00.115 - INFO: Logfile created: 2018_04_19/SensorDataProcessor_2018_04_19_0000.log
19.04.2018 00:00:00.116 - INFO: LogLevel is currently set to Information
19.04.2018 00:00:00.136 - INFO: Logfile created: 2018_04_19/SensorDataProcessor_2018_04_19_0000.log
19.04.2018 00:00:00.137 - INFO: LogLevel is currently set to Information
19.04.2018 00:00:00.364 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:00.365 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:01.434 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:00.684 Value=259.2 ElapsedTime=566
19.04.2018 00:00:02.348 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:01.688 Value=532.9 ElapsedTime=444
19.04.2018 00:00:05.643 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:04.660 Value=446 ElapsedTime=636
19.04.2018 00:00:06.552 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:05.660 Value=32 ElapsedTime=522
19.04.2018 00:00:09.779 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:08.682 Value=259.2 ElapsedTime=651
19.04.2018 00:00:10.255 - INFO: Processed SensorData: Id=2301002 Timestamp=19.04.2018 00:00:08.976 Value=0 ElapsedTime=604
19.04.2018 00:00:10.841 - INFO: Processed SensorData: Id=2002100 Timestamp=19.04.2018 00:00:09.383 Value=0 ElapsedTime=445
19.04.2018 00:00:10.773 - INFO: Processed SensorData: Id=2002001 Timestamp=19.04.2018 00:00:09.205 Value=1 ElapsedTime=644
19.04.2018 00:00:11.079 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:11.082 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:11.479 - INFO: Processed SensorData: Id=2005001 Timestamp=19.04.2018 00:00:09.404 Value=14 ElapsedTime=473
19.04.2018 00:00:11.758 - INFO: Processed SensorData: Id=2006001 Timestamp=19.04.2018 00:00:09.515 Value=15.3 ElapsedTime=677
19.04.2018 00:00:12.059 - INFO: Processed SensorData: Id=1005004 Timestamp=19.04.2018 00:00:09.677 Value=532.3 ElapsedTime=449
19.04.2018 00:00:12.762 - INFO: Processed SensorData: Id=2002102 Timestamp=19.04.2018 00:00:09.558 Value=0 ElapsedTime=561
19.04.2018 00:00:12.714 - INFO: Processed SensorData: Id=2006003 Timestamp=19.04.2018 00:00:09.517 Value=0 ElapsedTime=672
19.04.2018 00:00:13.168 - <<EXCEPTION>>: AggregateException from CloudAppendBlob.AppendTextAsync
19.04.2018 00:00:13.171 - <<EXCEPTION>>: Type =<System.AggregateException> Message =<One or more errors occurred. (One or more errors occurred. (The append position condition specified was not met.))> InnerException.Message=<One or more errors occurred. (The append position condition specified was not met.)>
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.<Dispose>b__8_0()
   at Microsoft.WindowsAzure.Storage.Core.Util.CommonUtility.RunWithoutSynchronizationContext(Action actionToRun)
   at Microsoft.WindowsAzure.Storage.Blob.BlobWriteStream.Dispose(Boolean disposing)
   at System.IO.Stream.Close()
   at Microsoft.WindowsAzure.Storage.Blob.CloudAppendBlob.<UploadFromStreamAsyncHelper>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Infrastructure.Logging.AzureBlob.Logger.<MessageLogger>d__24.MoveNext() in /opt/vsts/work/1/s/Infrastructure/Logging/Infrastructure.Logging.AzureBlob/Logger.cs:line 183
19.04.2018 00:00:13.381 - INFO: Processed SensorData: Id=2003001 Timestamp=19.04.2018 00:00:09.663 Value=1.1 ElapsedTime=483
19.04.2018 00:00:13.749 - INFO: Processed SensorData: Id=2003100 Timestamp=19.04.2018 00:00:09.779 Value=0 ElapsedTime=761

В чем может быть причина возникновения этого исключения?

Как я могу решить эту проблему дальше?

0
OlavT 26 Апр 2018 в 17:17

1 ответ

Лучший ответ

Заголовок документации CloudAppendBlob.AppendTextAsync () упоминает:

Этот API следует использовать строго в сценарии с одной записью, поскольку API внутренне использует условный заголовок append-offset, чтобы избежать дублирования блоков, что не работает в сценарии с несколькими модулями записи.

Если вам действительно нужна поддержка сценария с несколькими авторами, в котором порядок не важен, я бы посоветовал вам использовать CloudAppendBlob.AppendBlockAsync ().

0
Zhaoxing Lu 27 Апр 2018 в 02:40