Exemplo n.º 1
0
        public async Task <T> ExecuteReaderAsync <T>(string script, Action <DbCommand> setParams,
                                                     Func <DbDataReader, CancellationToken, Task <T> > callbackAsync)
        {
            using (var op = SnTrace.Database.StartOperation(GetOperationMessage("ExecuteReaderAsync", script)))
            {
                try
                {
                    using (var cmd = CreateCommand())
                    {
                        cmd.Connection     = OpenConnection();
                        cmd.CommandTimeout = Configuration.Data.DbCommandTimeout;
                        cmd.CommandText    = script;
                        cmd.CommandType    = CommandType.Text;
                        cmd.Transaction    = _transaction?.Transaction;

                        setParams?.Invoke(cmd);

                        var cancellationToken = _transaction?.CancellationToken ?? _cancellationToken;
                        using (var reader = (DbDataReader)await cmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false))
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                            var result = await callbackAsync(reader, cancellationToken).ConfigureAwait(false);

                            op.Successful = true;
                            return(result);
                        }
                    }
                }
                catch (Exception e)
                {
                    SnTrace.WriteError(e.ToString());
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        public void SnTrace_WriteError()
        {
            CleanupAndEnableAll();

            SnTrace.WriteError("asdf");

            var log = DisableAllAndGetLog();

            Assert.AreEqual(1, log.Count);
            Assert.AreEqual("ERROR", GetColumnFromLine(log[0], 6));
            Assert.IsTrue(log[0].EndsWith("asdf"));
        }