示例#1
0
 public LogEntry(LogEntry prototype)
 {
     this.Timestamp = prototype.Timestamp;
     this.Status = prototype.Status;
     this.GroupKey = prototype.GroupKey;
     this.TargetKey = prototype.TargetKey;
     this.InstanceKey = prototype.InstanceKey;
     this.LogContent = prototype.LogContent;
 }
示例#2
0
        public static LogEntry Parse(XmlReader source)
        {
            XDocument doc;
            try
            {
                doc = XDocument.Load(source);
            }
            catch (Exception ex)
            {
                throw new DeserialisationException("Failed deserialising log entry.", ex);
            }

            if (!ValidateInstanceXml(doc))
                throw new DeserialisationException("Serialised log entry xml is not valid.");

            DateTime timestamp;
            LogStatus status;
            Guid groupKey, targetKey, instanceKey;

            if (!DateTime.TryParse(doc.Root.Attribute("timestamp").Value, out timestamp))
                throw new DeserialisationException("Serialised log entry timestamp is not a valid datetime.");
            if (!Enum.TryParse(doc.Root.Attribute("status").Value, out status))
                throw new DeserialisationException("Serialised log entry status is not a valid log status.");
            if (!Guid.TryParse(doc.Root.Element("groupKey").Value, out groupKey))
                throw new DeserialisationException("Serialised log entry group key is not a valid guid.");
            if (!Guid.TryParse(doc.Root.Element("targetKey").Value, out targetKey))
                throw new DeserialisationException("Serialised log entry target key is not a valid guid.");
            if (!Guid.TryParse(doc.Root.Element("instanceKey").Value, out instanceKey))
                throw new DeserialisationException("Serialised log entry instance key is not a valid guid.");

            var logEntry = new LogEntry()
            {
                Timestamp = timestamp.ToUniversalTime(),
                Status = status,
                GroupKey = groupKey,
                TargetKey = targetKey,
                InstanceKey = instanceKey,
                LogContent = doc.Root.Element("logContent").Value,
            };

            return logEntry;
        }
示例#3
0
        public static Stream Serialise(LogEntry logEntry)
        {
            if (logEntry == null)
                throw new ArgumentNullException("logEntry", "Version cannot be null.");

            var doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", "yes"),
                new XElement("logEntry",
                    new XAttribute("timestamp", logEntry.Timestamp),
                    new XAttribute("status", logEntry.Status),
                    new XElement("groupKey", logEntry.GroupKey),
                    new XElement("targetKey", logEntry.TargetKey),
                    new XElement("instanceKey", logEntry.InstanceKey),
                    new XElement("logContent", logEntry.LogContent)));

            return Serialisation.Serialise(doc);
        }
示例#4
0
        public void AddLogEntry(LogEntry logEntry)
        {
            if (logEntry == null)
                throw new ArgumentNullException("logEntry", "You cannot add a null log entry.");
            if (logEntry.InstanceKey == Guid.Empty)
                throw new ArgumentException("Instance key must be set to a non-empty guid.");

            using (var stream = logEntry.Serialise())
            {
                var instancesController = new Instances(Context);
                if (!instancesController.InstanceExists(logEntry.InstanceKey))
                    throw new InstanceNotFoundException(String.Format("Instance with the key \"{0}\" could not be found.", logEntry.InstanceKey));

                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        var entryPath = GetInstanceLogEntryPath(logEntry.InstanceKey, logEntry.Timestamp, logEntry.Status);

                        using (var putResponse = client.PutObject(new PutObjectRequest()
                        {
                            BucketName = Context.BucketName,
                            Key = entryPath,
                            InputStream = stream,
                        })) { }
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed creating log entry.", awsEx);
                }
            }
        }