public void SqlServerLogWriter_Write_UsesEventFormatter()
        {
            TruncateTable();

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

            mapping.Add("DateTime", "LogDate");
            mapping.Add("EventName", "Event");
            mapping.Add("EventType", "EventType");
            mapping.Add("Source", "Source");
            mapping.Add("SeverityLevel", "Severity");
            mapping.Add("Test Prop", "Data");
            mapping.Add("FullDetails", "StructuredData");

            var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", mapping, Formatters.XmlLogEventFormatter.DefaultInstance, true);

            writer.Write(new LogEvent()
            {
                EventName  = "Test event",
                DateTime   = DateTime.Now,
                Source     = "Test Source",
                Properties = new Dictionary <string, object>()
                {
                    { "Test Prop", "Test Value" }
                }
            });

            var data     = GetTableRows();
            var eventDoc = System.Xml.Linq.XDocument.Parse((string)data.Tables[0].Rows[0]["StructuredData"]);

            Assert.IsNotNull(eventDoc);
            Assert.IsNotNull((from n in eventDoc.Descendants() where n.Name == "LogEvent" select n).FirstOrDefault());
        }
        public void SqlServerLogWriter_Write_WritesEvent()
        {
            TruncateTable();

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

            mapping.Add("DateTime", "LogDate");
            mapping.Add("EventName", "Event");
            mapping.Add("EventType", "EventType");
            mapping.Add("Source", "Source");
            mapping.Add("SeverityLevel", "Severity");
            mapping.Add("Test Prop", "Data");
            mapping.Add("FullDetails", "StructuredData");

            var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", mapping);

            writer.Write(new LogEvent()
            {
                EventName  = "Test event",
                DateTime   = DateTime.Now,
                Source     = "Test Source",
                Properties = new Dictionary <string, object>()
                {
                    { "Test Prop", "Test Value" }
                }
            });

            var data = GetTableRows();

            Assert.AreEqual(1, data.Tables[0].Rows.Count);
            var logEvent = Newtonsoft.Json.JsonConvert.DeserializeObject <LogEvent>((string)data.Tables[0].Rows[0]["StructuredData"]);

            Assert.AreEqual("Test event", logEvent.EventName);
            Assert.AreEqual("Test Source", logEvent.Source);
        }
示例#3
0
        public RequestResponseLoggingMiddleware(RequestDelegate next,
                                                ILoggerFactory loggerFactory,
                                                ILogRecord record,
                                                IConfiguration configuration)
        {
            _next          = next;
            _logger        = loggerFactory.CreateLogger <RequestResponseLoggingMiddleware>();
            _configuration = configuration;
            _record        = record;
            var connectionString = _configuration.GetConnectionString(AppConst.DefaultDb);

            _writer = new SqlServerLogWriter(connectionString);
        }
        public void SqlServerLogWriter_WriteBatch_TruncatesOverSizeStrings()
        {
            TruncateTable();

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

            mapping.Add("DateTime", "LogDate");
            mapping.Add("EventName", "Event");
            mapping.Add("EventType", "EventType");
            mapping.Add("Source", "Source");
            mapping.Add("SeverityLevel", "Severity");
            mapping.Add("Test Prop", "Data");
            mapping.Add("FullDetails", "StructuredData");

            var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", mapping, Formatters.XmlLogEventFormatter.DefaultInstance, true);
            var source = new string('A', 512);

            var entryCount = 1000;
            var events     = new LogEvent[entryCount];

            for (int cnt = 0; cnt < entryCount; cnt++)
            {
                events[cnt] = new LogEvent()
                {
                    EventName  = "Test event " + cnt.ToString(),
                    DateTime   = DateTime.Now,
                    Source     = source,
                    Properties = new Dictionary <string, object>()
                    {
                        { "Test Prop", "Test Value" }
                    }
                };
            }

            writer.WriteBatch(events, 10);

            string truncatedSoruce = new string('A', 255);
            var    data            = GetTableRows();

            foreach (System.Data.DataRow row in data.Tables[0].Rows)
            {
                Assert.AreEqual(truncatedSoruce, (string)row["Source"]);
            }
        }
        public void SqlServerLogWriter_WriteBatch_WritesPartialArrayBatch()
        {
            TruncateTable();

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

            mapping.Add("DateTime", "LogDate");
            mapping.Add("EventName", "Event");
            mapping.Add("EventType", "EventType");
            mapping.Add("Source", "Source");
            mapping.Add("SeverityLevel", "Severity");
            mapping.Add("Test Prop", "Data");

            var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", mapping);

            var entryCount = 1000;
            var events     = new LogEvent[entryCount];

            for (int cnt = 0; cnt < entryCount; cnt++)
            {
                events[cnt] = new LogEvent()
                {
                    EventName  = "Test event " + cnt.ToString(),
                    DateTime   = DateTime.Now,
                    Source     = "Test Source",
                    Properties = new Dictionary <string, object>()
                    {
                        { "Test Prop", "Test Value" }
                    }
                };
            }

            var sw = new System.Diagnostics.Stopwatch();

            sw.Start();
            writer.WriteBatch(events, 10);
            sw.Stop();
            System.Diagnostics.Trace.WriteLine($"SqlServerLogWriter: Write of {10} entries took {sw.Elapsed.ToString()}");

            var data = GetTableRows();

            Assert.AreEqual(10, data.Tables[0].Rows.Count);
        }
 public void SqlServerLogWriter_Constructor_ConstructsOkWithNullFormatter()
 {
     var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", null, null, true);
 }
 public void SqlServerLogWriter_Constructor_ConstructsOkWithNullColumnMapping()
 {
     var writer = new SqlServerLogWriter(ConnectionString, "AuditTrail", null);
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnWhitespaceTableName()
 {
     var writer = new SqlServerLogWriter(ConnectionString, "  ");
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnEmptyTableName()
 {
     var writer = new SqlServerLogWriter(ConnectionString, String.Empty);
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnNullTableName()
 {
     var writer = new SqlServerLogWriter(ConnectionString, null);
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnWhitespaceConnectionString()
 {
     var writer = new SqlServerLogWriter("  ", "Test");
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnEmptyConnectionString()
 {
     var writer = new SqlServerLogWriter(String.Empty, "Test");
 }
 public void SqlServerLogWriter_Constructor_ThrowsOnNullConnectionString()
 {
     var writer = new SqlServerLogWriter(null, "Test");
 }