Semantic Logging Not logging to the Database in the Latest version

Topics: Semantic Logging Application Block
Sep 12, 2014 at 8:29 PM
Edited Sep 12, 2014 at 8:30 PM
I was testing the Semantic Logging Block to log to SQL Server but no records show up until I revert the packages 2 version back. With no code change.

With these packages no records get into the database

EnterpriseLibrary.SemanticLogging version=2.0.1406.1
EnterpriseLibrary.SemanticLogging.Database version=2.0.1406.1
EnterpriseLibrary.TransientFaultHandling version=6.0.1304.0
EnterpriseLibrary.TransientFaultHandling.Data version=6.0.1304.1
Newtonsoft.Json version=5.0.8

With these packages records get into the database

EnterpriseLibrary.SemanticLogging version=1.0.1304.0
EnterpriseLibrary.SemanticLogging.Database version=1.0.1304.0
EnterpriseLibrary.TransientFaultHandling version=6.0.1304.0
EnterpriseLibrary.TransientFaultHandling.Data version=6.0.1304.0
Newtonsoft.Json version=4.5.1

Configuration File

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="Tracing" connectionString="Data Source=XXXXXXXXXX;Initial Catalog=Logging;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>

Event Source

using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoggingE6.Instrumentation
{
[EventSource(Name = "E6EventSource")]
public class E6EventSource : EventSource
{
    public static class Keywords
    {
        public const EventKeywords Application = (EventKeywords)1L;
        public const EventKeywords DataAccess = (EventKeywords)2L;
        public const EventKeywords UserInterface = (EventKeywords)4L;
        public const EventKeywords General = (EventKeywords)8L;
    }

    public static class Tasks
    {
        //public const EventTask LoadUser = (EventTask)1;
        //public const EventTask LoadExpenses = (EventTask)2;
        //public const EventTask LoadAllExpenses = (EventTask)3;
        //public const EventTask SaveExpense = (EventTask)4;
        //public const EventTask GetExpensesForApproval = (EventTask)5;
        //public const EventTask Initialize = (EventTask)6;
        //public const EventTask ApproveExpense = (EventTask)8;

        public const EventTask Initialize = (EventTask)1;
        public const EventTask Tracing = (EventTask)2;
    }

    public static class Opcodes
    {
        public const EventOpcode Start = (EventOpcode)20;
        public const EventOpcode Finish = (EventOpcode)21;
        public const EventOpcode Error = (EventOpcode)22;
        public const EventOpcode Starting = (EventOpcode)23;

        //public const EventOpcode QueryStart = (EventOpcode)30;
        //public const EventOpcode QueryFinish = (EventOpcode)31;
        //public const EventOpcode QueryNoResults = (EventOpcode)32;

        //public const EventOpcode CacheQuery = (EventOpcode)40;
        //public const EventOpcode CacheUpdate = (EventOpcode)41;
        //public const EventOpcode CacheHit = (EventOpcode)42;
        //public const EventOpcode CacheMiss = (EventOpcode)43;
    }

    public static readonly E6EventSource Log = new E6EventSource();

    #region Application

    [Event(100, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
    public void ApplicationStarting()
    {
        if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
        {
            this.WriteEvent(100);
        }
    }

    [Event(101, Level = EventLevel.Informational, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Start, Version = 1)]
    public void ApplicationStarted()
    {
        if (this.IsEnabled(EventLevel.Informational, Keywords.Application))
        {
            this.WriteEvent(101);
        }
    }

    [Event(102, Level = EventLevel.Error, Keywords = Keywords.Application, Task = Tasks.Tracing, Opcode = Opcodes.Error, Version = 1)]
    public void ApplicationError(string exceptionMessage, string exceptionType)
    {
        if (this.IsEnabled(EventLevel.Error, Keywords.Application))
        {
            this.WriteEvent(102, exceptionMessage, exceptionType);
        }
    }

    [Event(103, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
    public void ApplicationEnding()
    {
        if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
        {
            this.WriteEvent(103);
        }
    }

    [Event(104, Level = EventLevel.Verbose, Keywords = Keywords.Application, Task = Tasks.Initialize, Opcode = Opcodes.Starting, Version = 1)]
    public void ApplicationEnd()
    {
        if (this.IsEnabled(EventLevel.Verbose, Keywords.Application))
        {
            this.WriteEvent(104);
        }
    }

    #endregion
}
}

Console Application

using LoggingE6.Instrumentation;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging;
using Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Sinks;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics.Tracing;

namespace LoggingE6
{
class Program
{
    static void Main(string[] args)
    {
        var listener1 = new ObservableEventListener();

        listener1.EnableEvents(E6EventSource.Log, EventLevel.Verbose, Keywords.All);

        SinkSubscription<SqlDatabaseSink> subscription =
            listener1.LogToSqlDatabase("TestE6",
            ConfigurationManager.ConnectionStrings["Tracing"].ConnectionString,
            "Traces",
            Microsoft.Practices.EnterpriseLibrary.SemanticLogging.Utility.Buffering.DefaultBufferingInterval,
            1, // it shuld get log after every message
            Timeout.InfiniteTimeSpan,
            500);

        for (int i = 0; i < 10; i++)
        {
            E6EventSource.Log.ApplicationStarted();

        }


        // Im sure that connection string & other settings are correct 
        // because it logs into DB if i explicitly flush or dispose listener as below
        // subscription.Sink.FlushAsync().Wait();            
        listener1.Dispose();

        Console.ReadLine();
    }
}
}
Sep 15, 2014 at 5:57 AM
Did you apply the database changes required for version 2.0? There are additional columns added in version 2.0 that could cause errors if the script wasn't run. The script is located in the packages\EnterpriseLibrary.SemanticLogging.Database.2.0.1406.1\scripts directory.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 15, 2014 at 12:54 PM
Randy,

Thanks that did the trick. I was still using the database from 1.0.

Thanks Joe