<commit_before>﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Autofac;
using Microsoft.Practices.TransientFaultHandling;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using NHibernate;
using NHibernate.Driver;
using NHibernate.SqlAzure;
using SQLAzureDemo.App_Start.NHibernate;
using Autofac.Integration.Mvc;
using SQLAzureDemo.App_Start.Serilog;

namespace SQLAzureDemo.App_Start.Autofac
{
    public class NHibernateModule : Module
    {
        public const string TransientConnection = "transient";
        public const string ResilientConnection = "resilient";

        private readonly string _connectionString;

        public NHibernateModule(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected override void Load(ContainerBuilder builder)
        {
            Register<Sql2008ClientDriver>(builder, TransientConnection);
            Register<LoggingSqlAzureClientDriverWithTimeoutRetries>(builder, ResilientConnection);
        }

        private void Register<TDriver>(ContainerBuilder builder, string key)
            where TDriver: IDriver
        {
            builder.Register(c => new NHibernateConfiguration<TDriver>(_connectionString).GetSessionFactory())
                .Keyed<ISessionFactory>(key)
                .SingleInstance();

            builder.Register(c => c.ResolveKeyed<ISessionFactory>(key).OpenSession())
                .Keyed<ISession>(key)
                .InstancePerHttpRequest();
        }
    }

    public class LoggingSqlAzureClientDriverWithTimeoutRetries : SqlAzureClientDriverWithTimeoutRetries
    {
        private static CloudTable _table;
        public static void Initialise(CloudStorageAccount storageAccount)
        {
            var tableClient = storageAccount.CreateCloudTableClient();
            _table = tableClient.GetTableReference(typeof(TransientRetry).Name);
            _table.CreateIfNotExists();
        }

        protected override EventHandler<RetryingEventArgs> RetryEventHandler()
        {
            return (sender, args) => _table.Execute(TableOperation.Insert(new TransientRetry(args)));
        }

        public class TransientRetry : TableEntity
        {
            public TransientRetry() {}

            public TransientRetry(RetryingEventArgs eventArgs)
            {
                var lastException = eventArgs.LastException;

                Delay = eventArgs.Delay.ToString();
                RetryCount = eventArgs.CurrentRetryCount;
                Exception = lastException.TraceInformation();
                ExceptionMessage = lastException.Message;
                
                Url = HttpContext.Current.Request.RawUrl;
                Server = Environment.MachineName;

                var sqlException = lastException as SqlException;
                if (sqlException != null)
                    SqlErrorNumbers = string.Join(",", sqlException.Errors.Cast<SqlError>().Select(e => e.Number));

                PartitionKey = string.Format("0{0}", DateTime.UtcNow.Ticks);
                RowKey = string.Format("{0}|{1}", SqlErrorNumbers, RetryCount);<commit_msg>Added proper url for the current page<commit_after>﻿using System;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using Autofac;
using Microsoft.Practices.TransientFaultHandling;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using NHibernate;
using NHibernate.Driver;
using NHibernate.SqlAzure;
using SQLAzureDemo.App_Start.NHibernate;
using Autofac.Integration.Mvc;
using SQLAzureDemo.App_Start.Serilog;

namespace SQLAzureDemo.App_Start.Autofac
{
    public class NHibernateModule : Module
    {
        public const string TransientConnection = "transient";
        public const string ResilientConnection = "resilient";

        private readonly string _connectionString;

        public NHibernateModule(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected override void Load(ContainerBuilder builder)
        {
            Register<Sql2008ClientDriver>(builder, TransientConnection);
            Register<LoggingSqlAzureClientDriverWithTimeoutRetries>(builder, ResilientConnection);
        }

        private void Register<TDriver>(ContainerBuilder builder, string key)
            where TDriver: IDriver
        {
            builder.Register(c => new NHibernateConfiguration<TDriver>(_connectionString).GetSessionFactory())
                .Keyed<ISessionFactory>(key)
                .SingleInstance();

            builder.Register(c => c.ResolveKeyed<ISessionFactory>(key).OpenSession())
                .Keyed<ISession>(key)
                .InstancePerHttpRequest();
        }
    }

    public class LoggingSqlAzureClientDriverWithTimeoutRetries : SqlAzureClientDriverWithTimeoutRetries
    {
        private static CloudTable _table;
        public static void Initialise(CloudStorageAccount storageAccount)
        {
            var tableClient = storageAccount.CreateCloudTableClient();
            _table = tableClient.GetTableReference(typeof(TransientRetry).Name);
            _table.CreateIfNotExists();
        }

        protected override EventHandler<RetryingEventArgs> RetryEventHandler()
        {
            return (sender, args) => _table.Execute(TableOperation.Insert(new TransientRetry(args)));
        }

        public class TransientRetry : TableEntity
        {
            public TransientRetry() {}

            public TransientRetry(RetryingEventArgs eventArgs)
            {
                var lastException = eventArgs.LastException;

                Delay = eventArgs.Delay.ToString();
                RetryCount = eventArgs.CurrentRetryCount;
                Exception = lastException.TraceInformation();
                ExceptionMessage = lastException.Message;
                
                Url = HttpContext.Current.Request.Url.ToString();
                Server = Environment.MachineName;

                var sqlException = lastException as SqlException;
                if (sqlException != null)
                    SqlErrorNumbers = string.Join(",", sqlException.Errors.Cast<SqlError>().Select(e => e.Number));

                PartitionKey = string.Format("0{0}", DateTime.UtcNow.Ticks);
                RowKey = string.Format("{0}|{1}", SqlErrorNumbers, RetryCount);
{'fraction_non_alphanumeric': 0.0808504034761018, 'fraction_numerical': 0.002482929857231533, 'mean_word_length': 2.645361990950226, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7583353', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1559, 'n_words': 307}