Beispiel #1
0
        public void Test_EndToEnd()
        {
            SequencePropertyInitializer initializer = new SequencePropertyInitializer();

            EventTelemetry tele1 = new EventTelemetry("Event1");

            initializer.Initialize(tele1);
            EventTelemetry tele2 = new EventTelemetry("Event2");

            initializer.Initialize(tele2);
            EventTelemetry tele3 = new EventTelemetry("Event3");

            initializer.Initialize(tele3);

            Assert.AreEqual(1, SequenceIdParser.GetOrder(tele1.Sequence));
            Assert.AreEqual(2, SequenceIdParser.GetOrder(tele2.Sequence));
            Assert.AreEqual(3, SequenceIdParser.GetOrder(tele3.Sequence));

            var prefix      = SequenceIdParser.GetPrefix(tele1.Sequence);
            var prefixAgain = SequenceIdParser.GetPrefix(prefix);

            Assert.IsTrue(!string.IsNullOrEmpty(prefix));

            Assert.AreEqual(prefix, prefixAgain);

            Assert.AreEqual(prefix, SequenceIdParser.GetPrefix(tele2.Sequence));
            Assert.AreEqual(prefix, SequenceIdParser.GetPrefix(tele3.Sequence));
        }
        public void AISerializes_To_Expected_JSON()
        {
            string expectedJSON = "{\"name\":\"Microsoft.ApplicationInsights.instkey.Message\",\"time\":\"0001-01-01T00:00:00.0000000Z\",\"seq\":\"seq0\",\"iKey\":\"instkey\",\"tags\":{\"ai.cloud.roleInstance\":\"role.intance\",\"ai.application.ver\":\"compname.version\"},\"data\":{\"baseType\":\"MessageData\",\"baseData\":{\"ver\":2,\"message\":\"message text\",\"severityLevel\":\"Warning\",\"properties\":{\"key1\":\"value1\"}}}}" +
                                  "\r\n{\"name\":\"Microsoft.ApplicationInsights.instkey.Message\",\"time\":\"0001-01-01T00:00:00.0000000Z\",\"seq\":\"seq1\",\"iKey\":\"instkey\",\"tags\":{\"ai.cloud.roleInstance\":\"role.intance\",\"ai.application.ver\":\"compname.version\"},\"data\":{\"baseType\":\"MessageData\",\"baseData\":{\"ver\":2,\"message\":\"message text\",\"severityLevel\":\"Warning\",\"properties\":{\"key1\":\"value1\"}}}}" +
                                  "\r\n{\"name\":\"Microsoft.ApplicationInsights.instkey.Message\",\"time\":\"0001-01-01T00:00:00.0000000Z\",\"seq\":\"seq2\",\"iKey\":\"instkey\",\"tags\":{\"ai.cloud.roleInstance\":\"role.intance\",\"ai.application.ver\":\"compname.version\"},\"data\":{\"baseType\":\"MessageData\",\"baseData\":{\"ver\":2,\"message\":\"message text\",\"severityLevel\":\"Warning\",\"properties\":{\"key1\":\"value1\"}}}}" +
                                  "\r\n{\"name\":\"Microsoft.ApplicationInsights.instkey.Message\",\"time\":\"0001-01-01T00:00:00.0000000Z\",\"seq\":\"seq3\",\"iKey\":\"instkey\",\"tags\":{\"ai.cloud.roleInstance\":\"role.intance\",\"ai.application.ver\":\"compname.version\"},\"data\":{\"baseType\":\"MessageData\",\"baseData\":{\"ver\":2,\"message\":\"message text\",\"severityLevel\":\"Warning\",\"properties\":{\"key1\":\"value1\"}}}}" +
                                  "\r\n{\"name\":\"Microsoft.ApplicationInsights.instkey.Message\",\"time\":\"0001-01-01T00:00:00.0000000Z\",\"seq\":\"seq4\",\"iKey\":\"instkey\",\"tags\":{\"ai.cloud.roleInstance\":\"role.intance\",\"ai.application.ver\":\"compname.version\"},\"data\":{\"baseType\":\"MessageData\",\"baseData\":{\"ver\":2,\"message\":\"message text\",\"severityLevel\":\"Warning\",\"properties\":{\"key1\":\"value1\"}}}}";


            var props = new Dictionary <string, string>();

            props.Add("key1", "value1");
            var items = new List <ITelemetry>();

            var context = new TelemetryContext();

            context.InstrumentationKey = "instkey";
            context.Component.Version  = "compname.version";
            context.Cloud.RoleInstance = "role.intance";
            var initializer = new SequencePropertyInitializer();

            for (int i = 0; i < 5; i++)
            {
                var data      = new MessageDataModel();
                var telemetry = new MessageTelemetry("message text", eSeverityLevel.Warning, context, data, props);
                initializer.Initialize(telemetry);
                telemetry.Sanitize();
                telemetry.Sequence = string.Format("seq{0}", i);
                items.Add(telemetry);
            }

            var serializer = new AITelemetrySerializer(new JsonWriterFactory(), new AIContextTagKeys());
            var json       = Encoding.UTF8.GetString(serializer.Serialize(items, false)); //do not use compression.

            Assert.AreEqual(expectedJSON, json);
        }