Monday, 28 November 2011

AppFabric Event Logger Custom SQL Bulk Copy

I recently needed to customize the AppFabric logger to write to a different location apart from a SQL Database. Sadly to say out of the box it only comes with one provider. When configuring AppFabric in IIS Microsoft documentation states that you can provide your own providers.

Sadly to say I was not able to have 2 providers side by side and choose one from the other. Also writing a provider is not a simple task. So what I choose to do was implement my own IBulkCopy implementation. Essentially the three properties and WriteToServer completed the implementation needed for IBulk Copy.

To register my own AppFabricBukCopyProvider I essentially edited the Machine.Config as provided my own implementation dll.

Note: The lockElements of the machine.config forces the subsequent application app.config or web.config not to use bulkCopyProviders so I had little choice but to edit the machine.config.

Here is a sample

Edit Machine.Config

    <monitoring lockElements="bulkCopyProviders, collectors">
        <!-— ORIGINAL ITEM <bulkCopyProvider providerName="System.Data.SqlClient" type="Microsoft.ApplicationServer.Monitoring.EventCollector.SqlServerBulkCopy, Microsoft.ApplicationServer.Monitoring, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> -->
       <!-- Your own implementation -->
      <bulkCopyProvider providerName="System.Data.SqlClient" type="Helper.Provider.AppFabricBulkCopyProvider, Helper, Version=, Culture=neutral, PublicKeyToken=5251f5c14327d259" />

Here is the AppFabricBulkCopyProvider written in C#.
If you are aware of other ways of implementing this please let me know as I am interested.


    public class AppFabricBulkCopyProvider : IBulkCopy
        public int BatchSize { get; set; }
        public DbConnection Connection { get; set; }
        public string DestinationTableName { get; set; }

        public void WriteToServer(System.Data.IDataReader dataReader)

                AppFabricData appfRequest = new AppFabricData();
                appfRequest.strTable = this.DestinationTableName;
                appfRequest.dtsTable = ConvertDataReadertoDataSet(dataReader);

                appfRequest.dtsTable.WriteXml(string.Format(@"C:\angelol\ MONITORING\{0}-{1}.txt", this.DestinationTableName, Guid.NewGuid().ToString()));

            catch (Exception Exc)
                string strError;

                if (Exc.InnerException == null)
                    strError = string.Format("AppFabric Logging ErrorHandling::Exception:{0}", Exc.ToString());
                    strError = string.Format("AppFabric Logging ErrorHandling::Exception:{0}-InnerException{1}", Exc.ToString(), Exc.InnerException.ToString());


        private DataSet ConvertDataReadertoDataSet(IDataReader reader)

            DataSet ds = new DataSet();
            DataTable dataTable = new DataTable(this.DestinationTableName);

            DataTable schemaTable = reader.GetSchemaTable();

            DataRow row;
            string columnName;
            DataColumn column;

            int count = schemaTable.Rows.Count;

            for (int i = 0; i < count; i++)
                row = schemaTable.Rows[i];
                columnName = (string)row["ColumnName"];
                column = new DataColumn(columnName, (Type)row["DataType"]);

            object[] values = new object[count];

                while (reader.Read())
                    dataTable.LoadDataRow(values, true);


            //return dataTable;
            return ds;

No comments:

Post a Comment