Exemplo n.º 1
0
 private void EnsureConnectionIsAlive(ref SqlServerConnection connection, string query)
 {
     try {
         using (var command = connection.CreateCommand()) {
             command.CommandText = query;
             command.ExecuteNonQuery();
         }
     }
     catch (Exception exception) {
         if (SqlHelper.ShouldRetryOn(exception))
         {
             SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection);
             if (!TryReconnect(ref connection, query))
             {
                 SqlLog.Error(exception, Strings.LogConnectionRestoreFailed);
                 throw;
             }
             SqlLog.Info(Strings.LogConnectionSuccessfullyRestored);
         }
         else
         {
             throw;
         }
     }
 }
Exemplo n.º 2
0
        public void SqlLogTest()
        {
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            SqlLog.Debug("Test message", null);
            SqlLog.Debug("Test message with parameter {0}", new object[] { 1 });
            SqlLog.Debug(new Exception("Some exception"), "Test message with parameter {0}", new object[] { 1 });
            SqlLog.Debug("Test message", new object[] { 1 });
            SqlLog.Debug("Test message {0}", null);
            SqlLog.Debug(new Exception("Some exeption"));
            SqlLog.Debug(null, new object[] { 1 });

            SqlLog.Info("Test message", null);
            SqlLog.Info("Test message with parameter {0}", new object[] { 1 });
            SqlLog.Info(new Exception("Some exception"), "Test message with parameter {0}", new object[] { 1 });
            SqlLog.Info("Test message", new object[] { 1 });
            SqlLog.Info("Test message {0}", null);
            SqlLog.Info(new Exception("Some exeption"));
            SqlLog.Info(null, new object[] { 1 });

            SqlLog.Warning("Test message", null);
            SqlLog.Warning("Test message with parameter {0}", new object[] { 1 });
            SqlLog.Warning(new Exception("Some exception"), "Test message with parameter {0}", new object[] { 1 });
            SqlLog.Warning("Test message", new object[] { 1 });
            SqlLog.Warning("Test message {0}", null);
            SqlLog.Warning(new Exception("Some exeption"));
            SqlLog.Warning(null, new object[] { 1 });

            SqlLog.Error("Test message", null);
            SqlLog.Error("Test message with parameter {0}", new object[] { 1 });
            SqlLog.Error(new Exception("Some exception"), "Test message with parameter {0}", new object[] { 1 });
            SqlLog.Error("Test message", new object[] { 1 });
            SqlLog.Error("Test message {0}", null);
            SqlLog.Error(new Exception("Some exeption"));
            SqlLog.Error(null, new object[] { 1 });

            SqlLog.FatalError("Test message", null);
            SqlLog.FatalError("Test message with parameter {0}", new object[] { 1 });
            SqlLog.FatalError(new Exception("Some exception"), "Test message with parameter {0}", new object[] { 1 });
            SqlLog.FatalError("Test message", new object[] { 1 });
            SqlLog.FatalError("Test message {0}", null);
            SqlLog.FatalError(new Exception("Some exeption"));
            SqlLog.FatalError(null, new object[] { 1 });

            Assert.IsTrue(File.Exists(filePath));
            Assert.AreEqual(File.ReadAllLines(filePath).Count(), 35);
        }
Exemplo n.º 3
0
        private async Task OpenWithCheckAsync(string checkQueryString, CancellationToken cancellationToken)
        {
            bool connectionChecked = false;
            bool restoreTriggered  = false;

            while (!connectionChecked)
            {
                cancellationToken.ThrowIfCancellationRequested();
                await base.OpenAsync(cancellationToken).ConfigureAwait(false);

                try {
                    using (var command = underlyingConnection.CreateCommand()) {
                        command.CommandText = checkQueryString;
                        await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
                    }
                    connectionChecked = true;
                }
                catch (Exception exception)
                {
                    if (SqlHelper.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            SqlLog.Error(exception, Strings.LogConnectionRestoreFailed);
                            throw;
                        }
                        SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection);

                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try
                        {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void OpenWithCheck(string checkQueryString)
        {
            bool connectionChecked = false;
            bool restoreTriggered  = false;

            while (!connectionChecked)
            {
                base.Open();
                try {
                    using (var command = underlyingConnection.CreateCommand()) {
                        command.CommandText = checkQueryString;
                        command.ExecuteNonQuery();
                    }
                    connectionChecked = true;
                }
                catch (Exception exception) {
                    if (SqlHelper.ShouldRetryOn(exception))
                    {
                        if (restoreTriggered)
                        {
                            SqlLog.Error(exception, Strings.LogConnectionRestoreFailed);
                            throw;
                        }
                        SqlLog.Warning(exception, Strings.LogGivenConnectionIsCorruptedTryingToRestoreTheConnection);

                        var newConnection = new SqlServerConnection(underlyingConnection.ConnectionString);
                        try
                        {
                            underlyingConnection.Close();
                            underlyingConnection.Dispose();
                        }
                        catch { }

                        underlyingConnection = newConnection;
                        restoreTriggered     = true;
                        continue;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }