示例#1
0
        public static void Serialize(Stream stream, object obj)
        {
            Debug.Assert(_state != null, "State can't be null");

            if (obj == null)
            {
                _state.InstanceCannotBeNull();
            }

            var description = _state.GetDescription(obj.GetType());

            if (description == null)
            {
                _state.TypeNotSupported(obj.GetType());
            }

            var writer = WriterPool.GetFormatter(stream, Encoding);

            try
            {
                writer.Write(description.TypeId);
                description.Serialize(writer, obj);
            }
            finally
            {
                WriterPool.ReleaseFormatter(writer);
            }
        }
示例#2
0
        public static void Serialize <T>(Stream stream, T obj)
        {
            Debug.Assert(_state != null, "State can't be null");

            if (obj == null)
            {
                _state.InstanceCannotBeNull();
            }

            var description = _state.GetDescription(RuntimeHelpers.GetHashCode(typeof(T)));

            if (description == null)
            {
                _state.TypeNotSupported(obj.GetType());
            }

            var writer = WriterPool.GetFormatter(stream, Encoding);

            try
            {
                writer.Write(description.TypeId);
                ((SpecializedTypeDescription <T>)description).SerializeHanlder(obj, writer);
            }
            finally
            {
                WriterPool.ReleaseFormatter(writer);
            }
        }
示例#3
0
        static LoggerService()
        {
            var writer = new WriterPool(new List <IWriter> {
                new FileLogWriter("rndFile2.txt"), new ConsoleWriter()
            });

            _logger = new LoggerBatch(writer);
        }
        public void WriteXml(XmlWriter writer)
        {
            Type type = m_data.GetType();

            {
                CustomRootWriter customWriter;
                WriterPool.AllocateOrCreate(out customWriter);
                var derivedName = MyObjectBuilderSerializer.GetSerializedName(type);
                customWriter.Init(derivedName, writer);

                MyObjectBuilderSerializer.GetSerializer(type).Serialize(customWriter, m_data);

                customWriter.Release();
                WriterPool.Deallocate(customWriter);
            }
        }
示例#5
0
        public void WriteXml(XmlWriter writer)
        {
            Type type = m_data.GetType();

            XmlSerializer    serializer  = MyXmlSerializerManager.GetOrCreateSerializer(type);
            var              derivedName = MyXmlSerializerManager.GetSerializedName(type);
            CustomRootWriter customWriter;

            WriterPool.AllocateOrCreate(out customWriter);
            customWriter.Init(derivedName, writer);

            serializer.Serialize(customWriter, m_data);

            customWriter.Release();
            WriterPool.Deallocate(customWriter);
        }
示例#6
0
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var test = query.Select(condition => condition ? 1 : Sleep(Options.InjectedWaitTime.TotalSeconds)).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(test);

                while (true)
                {
                    try
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        Logger.LogDebug(sql);
                        await Injector.InjectAsync(sql, CancellationToken.None);

                        stopwatch.Stop();
                        WriterPool.Return(writer);

                        return(stopwatch.Elapsed < Options.SuccessfulTime);
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }
示例#7
0
        static void Main(string[] args)
        {
            var writer = new ConsoleWriter();
            var logger = new Logger(writer);

            logger.Debug("First debug message");
            logger.Error("First error message");
            logger.Fatal("First fatal message");
            logger.Info("First info message");

            var writers = new WriterPool(new List <IWriter> {
                new ConsoleWriter()
            });
            var batchLogger = new BatchLogger(writers);

            Console.WriteLine();
            batchLogger.Debug("Batch debug");
            batchLogger.Error("Batch error");

            Console.WriteLine("Let's FLUSH ))");
            batchLogger.Flush();

            Console.ReadLine();
        }
        public async Task <bool> TestAsync(IQueryable <bool> query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }

            var expr = ErrorExpressionProvider.GetErrorExpression();

            var p    = Expression.Parameter(typeof(bool), "condition");
            var next = query.Provider.CreateQuery <long>(
                Expression.Call(
                    typeof(Queryable).GetMethods().Where(m => m.Name == nameof(System.Linq.Queryable.Select)).Single(m => !m.GetParameters()[1].ParameterType.ToString().Contains("Int32"))
                    .MakeGenericMethod(typeof(bool), typeof(long)),
                    query.Expression,
                    Expression.Lambda(
                        Expression.Condition(
                            p,
                            expr,
                            Expression.Constant(1L)
                            ),
                        p
                        )
                    )
                ).Take(1);

            using (var writer = WriterPool.Get())
            {
                var visitor = new MySqlQueryModelVisitor(writer);
                var sql     = visitor.Render(next);

                Logger.LogDebug(sql);

                while (true)
                {
                    try
                    {
                        using (var response = await Injector.InjectAsync(sql, CancellationToken.None))
                        {
                            WriterPool.Return(writer);
                            return(await ErrorDetector.ContainsErrorAsync(response));
                        }
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("timed out") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (HttpRequestException req) when(req.InnerException?.Message?.Contains("unrecognized response") ?? false)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                    catch (OperationCanceledException)
                    {
                        Logger.LogWarning($"Retrying...");
                        continue;
                    }
                }
            }
        }