/// <summary>
        /// Ensures the writer in a way how standard .net 2.0 XmlWriterTraceListener does it.
        /// </summary>
        /// <returns></returns>
        private bool EnsureWriter()
        {
            bool flag = true;
            if (writer == null)
            {
                flag = false;
                if (fileName == null)
                {
                    return flag;
                }

                string fullPath = Path.GetFullPath(fileName);
                string directoryName = Path.GetDirectoryName(fullPath);
                fileName = Path.GetFileName(fullPath);

                for (int i = 0; i < 2; i++)
                {
                    try
                    {
                        if (textWriterProvider == null)
                        {
                            textWriterProvider = new FileTextWriterProvider(
                                true, GetEncodingWithFallback(new UTF8Encoding(false)), 0x1000);
                        }
                        writer = textWriterProvider.CreateWriter(fullPath);
                        flag = true;
                        break;
                    }
                    catch (IOException)
                    {
                        fileName = Guid.NewGuid() + fileName;
                        fullPath = Path.Combine(directoryName, fileName);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        break;
                    }
                    catch (Exception)
                    {
                        break;
                    }
                }
                if (!flag)
                {
                    fileName = null;
                }
            }
            return flag;
        }
        private void CreateNewWriter()
        {
            if (writer != null)
            {
                if (xmlBlobWriter != null)
                {
                    xmlBlobWriter.Flush();
                }
                writer.Flush();
                writer.Close();
                //writer = null;
            }

            string targetFileName = fileStaticName + DateTime.UtcNow.ToString(fileDatetimePattern);
            string pathCandidate = null;

            int maxIter = 10;

            for (int i = 1; i < maxIter; i++)
            {
                if (!File.Exists(logRootLocation + targetFileName + "_" + i + ".xml"))
                {
                    pathCandidate = Path.Combine(logRootLocation,
                                                 targetFileName + "_" + i + ".xml");
                    break;
                }
            }
            if (pathCandidate == null)
            {
                // fallback name, uses guid
                pathCandidate = Path.Combine(logRootLocation,
                                             fileStaticName + Guid.NewGuid() + ".xml");
            }

            logFilePath = pathCandidate;

            if (textWriterProvider == null)
            {
                textWriterProvider = new FileTextWriterProvider(
                    true, GetEncodingWithFallback(new UTF8Encoding(false)), 0x1000);
            }

            writer = textWriterProvider.CreateWriter(logFilePath);

            if (logFileHelper == null)
            {
                logFileHelper = new LogFileHelper();
            }
            logFileHelper.MaxFileSizeBytes = maxFileSizeBytes;
            logFileHelper.FilePath = logFilePath;
        }