public void Format__CorrectlyFormatsAllPlaceholders()
        {
            var pattern = "ABC{P.1}{P.2} DEF {P.3}";

            var formatter = new PatternFormatter();
            formatter.Pattern = pattern;

            var de = new DiagnosticEvent();
            de.Properties.TryAddProperty("P.1", () => 1);
            de.Properties.TryAddProperty("P.2", () => 2);
            de.Properties.TryAddProperty("P.3", () => 3);

            var resut = formatter.Format(de, new TypeDescriptorCollection());

            Assert.AreEqual("ABC12 DEF 3", resut);
        }
示例#2
0
        public override void Write(Entities.DiagnosticEvent de)
        {
            if(Bundle != null)
            {
                if(Bundle.TryBundle(de))
                {
                    // event added to the bundle
                    // do nothing else
                    return;
                }
                else
                {
                    // event not added to bundle,
                    // proceed as usual
                }
            }

            // TODO: allow to specify smtp config in diag config rather than always using one from app.config

            // TODO: this is just a quick mockup to get it working for demo, must be rewriten properly

            PatternFormatter f = new PatternFormatter();

            var msg = new MailMessage();

            var toElements = To.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            for (int i = 0; i < toElements.Length; i++)
            {
                f = new PatternFormatter();
                f.Pattern = toElements[i];

                var toformatted = f.Format(de, new TypeDescriptorCollection());
                msg.To.Add(toformatted);
            }

            msg.From = new MailAddress(From);

            msg.Subject = Subject.Format(de, new TypeDescriptorCollection());

            msg.Body = BodyFormatter.Format(de, new TypeDescriptorCollection());

            if(!LogsToAttach.IsNullOrEmpty())
            {
                // todo: support multiple logs

                var config_ref = GlobalDiagnostics.GlobalLogger.Config;

                var sink = 
                    (from s in config_ref.SinkDefinitions
                         where s is FileSink
                         && s.Name == LogsToAttach
                         select s as FileSink).FirstOrDefault();


                var attachmentStream = new MemoryStream();

                using(var l = sink.FileLockingStrategy.AcquireLock(sink.LogFile.FullName))
                {                   
                    using (var fs = new FileStream(sink.SinkLocation.Location, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                    {
                        using(var gz = new GZipStream(attachmentStream, CompressionMode.Compress, leaveOpen: true))
                        {
                            fs.CopyTo(gz);
                        }
                    }
                }

                if(attachmentStream != null)
                {
                    attachmentStream.Seek(0, SeekOrigin.Begin);

                    var attachment = new Attachment(attachmentStream, sink.LogFile.Name + ".gz");
                    msg.Attachments.Add(attachment);
                }
            }


            SmtpClient.Send(msg);
        }