コード例 #1
0
        public virtual void Update()
        {
            const int ExceptionTitleMaxLength = 64;

            try
            {
                UpdateCore();
            }
            catch (Exception ex)
            {
                IndexerLogEntry logEntry = new IndexerLogEntry()
                {
                    Title = ex.Message,
                    Date = DateTime.UtcNow,
                    ExceptionDetails = ex.ToString()
                };

                if (logEntry.Title.Length > ExceptionTitleMaxLength)
                {
                    logEntry.Title = logEntry.Title.Substring(0, ExceptionTitleMaxLength);
                }

                // Remove any new lines from the metadata, otherwise you get a 403
                // back from the Azure SDK
                logEntry.Title = logEntry.Title.Replace("\r", string.Empty);
                logEntry.Title = logEntry.Title.Replace("\n", string.Empty);

                _logWriter.Write(logEntry);
            }
        }
コード例 #2
0
        public IResultSegment <IndexerLogEntry> ReadWithoutDetails(int maximumResults, string continuationToken)
        {
            BlobContinuationToken blobContinuationToken = BlobContinuationTokenSerializer.Deserialize(continuationToken);

            BlobResultSegment blobSegment;

            try
            {
                blobSegment = _logsContainer.ListBlobsSegmented(
                    prefix: _containerDirectory,
                    useFlatBlobListing: true,
                    blobListingDetails: BlobListingDetails.Metadata,
                    maxResults: maximumResults,
                    currentToken: blobContinuationToken,
                    options: null,
                    operationContext: null);
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFound())
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            if (blobSegment == null)
            {
                return(null);
            }

            List <IndexerLogEntry> results = new List <IndexerLogEntry>();

            // Cast from IListBlobItem to ICloudBlob is safe due to useFlatBlobListing: true above.
            foreach (ICloudBlob blob in blobSegment.Results)
            {
                IndexerLogEntry entry = ParseEntryFromBlobMetadata(blob);
                if (entry != null)
                {
                    results.Add(entry);
                }
            }

            string nextContinuationToken = BlobContinuationTokenSerializer.Serialize(blobSegment.ContinuationToken);

            return(new ResultSegment <IndexerLogEntry>(results, nextContinuationToken));
        }
コード例 #3
0
        public void Write(IndexerLogEntry entry)
        {
            if (entry == null)
            {
                return;
            }

            string logBlobName = BlobNames.GetConflictFreeDateTimeBasedBlobName(entry.Date);
            entry.Id = logBlobName;

            string logEntryAsJson = JsonConvert.SerializeObject(entry, JsonSerialization.Settings);

            _logsContainer.CreateIfNotExists();

            CloudBlockBlob logBlob = _logsContainer.GetBlockBlobReference(_containerDirectory + "/" + logBlobName);

            logBlob.Metadata[BlobLogEntryKeys.TitleKey] = entry.Title;
            logBlob.Metadata[BlobLogEntryKeys.LogDate] = entry.Date.ToString(CultureInfo.InvariantCulture);

            logBlob.UploadText(logEntryAsJson);
        }
コード例 #4
0
        /// <summary>
        /// Creates a log entry without details by only using information
        /// available on the blob itself, without looking at the content
        /// </summary>
        /// <param name="blob">The blob to parse</param>
        /// <returns>A log entry or null</returns>
        private IndexerLogEntry ParseEntryFromBlobMetadata(ICloudBlob blob)
        {
            if (blob == null ||
                !blob.Metadata.ContainsKey(BlobLogEntryKeys.LogDate) ||
                !blob.Metadata.ContainsKey(BlobLogEntryKeys.TitleKey))
            {
                return(null);
            }

            IndexerLogEntry entry = new IndexerLogEntry();

            entry.Id = blob.Name;
            if (_containerDirectory.Length != 0 && _containerDirectory.Length < entry.Id.Length)
            {
                entry.Id = entry.Id.Substring(_containerDirectory.Length + 1);
            }

            entry.Date  = DateTime.Parse(blob.Metadata[BlobLogEntryKeys.LogDate]);
            entry.Title = blob.Metadata[BlobLogEntryKeys.TitleKey];

            return(entry);
        }
コード例 #5
0
        public void Write(IndexerLogEntry entry)
        {
            if (entry == null)
            {
                return;
            }

            string logBlobName = BlobNames.GetConflictFreeDateTimeBasedBlobName(entry.Date);

            entry.Id = logBlobName;

            string logEntryAsJson = JsonConvert.SerializeObject(entry, JsonSerialization.Settings);

            _logsContainer.CreateIfNotExists();

            CloudBlockBlob logBlob = _logsContainer.GetBlockBlobReference(_containerDirectory + "/" + logBlobName);

            logBlob.Metadata[BlobLogEntryKeys.TitleKey] = entry.Title;
            logBlob.Metadata[BlobLogEntryKeys.LogDate]  = entry.Date.ToString(CultureInfo.InvariantCulture);

            logBlob.UploadText(logEntryAsJson);
        }
コード例 #6
0
        /// <summary>
        /// Creates a log entry without details by only using information
        /// available on the blob itself, without looking at the content
        /// </summary>
        /// <param name="blob">The blob to parse</param>
        /// <returns>A log entry or null</returns>
        private IndexerLogEntry ParseEntryFromBlobMetadata(ICloudBlob blob)
        {
            if (blob == null ||
                !blob.Metadata.ContainsKey(BlobLogEntryKeys.LogDate) ||
                !blob.Metadata.ContainsKey(BlobLogEntryKeys.TitleKey))
            {
                return null;
            }

            IndexerLogEntry entry = new IndexerLogEntry();
            entry.Id = blob.Name;
            if (_containerDirectory.Length != 0 && _containerDirectory.Length < entry.Id.Length)
            {
                entry.Id = entry.Id.Substring(_containerDirectory.Length + 1);
            }

            entry.Date = DateTime.Parse(blob.Metadata[BlobLogEntryKeys.LogDate], CultureInfo.InvariantCulture);
            entry.Title = blob.Metadata[BlobLogEntryKeys.TitleKey];

            return entry;
        }