public void Write(LogEntry entry)
 {
     if (Enabled)
     {
         FileStreamWriter.Write(Serializer.Serialize(entry));
     }
 }
 public void Dispose()
 {
     if (FileStreamWriter != null)
     {
         FileStreamWriter.Flush();
         FileStreamWriter.Dispose();
         Filename         = null;
         FileStreamWriter = null;
     }
 }
Beispiel #3
0
        /// <summary>
        /// 移动日志文件
        /// </summary>
        /// <returns>新的日志文件名称</returns>
        private string moveBak()
        {
            string fileName = fileStream.FileName;

            fileStream.Dispose();
            fileStream = null;
            try
            {
                return(AutoCSer.IO.File.MoveBak(fileName));
            }
            finally { tryOpen(); }
        }
        public void GetWriterSuccessTest()
        {
            //Arrange
            IFileStreamWriter fileStreamWriter = new FileStreamWriter();
            string            filePath         = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "TestFiles\\DummyFile.txt");
            FileInfo          fileInfo         = new FileInfo(filePath);

            //Act
            using (var streamWriter = fileStreamWriter.GetWriter(fileInfo))
            {
                //Assert
                Assert.IsTrue(streamWriter is StreamWriter, $"File: {fileInfo.FullName} could not be written to");
            }
        }
Beispiel #5
0
 /// <summary>
 /// 打开日志文件
 /// </summary>
 /// <param name="isTrace">是否输出调试信息</param>
 /// <returns>日志文件是否打开成功</returns>
 private bool open(bool isTrace)
 {
     try
     {
         fileStream = new FileStreamWriter(fileName, FileMode.OpenOrCreate, FileShare.Read, FileOptions.None, SubBuffer.Size.Kilobyte4, null, AutoCSer.Config.Pub.Default.EncodingCache);
         return(isFieStream = true);
     }
     catch (Exception error)
     {
         if (isTrace)
         {
             AutoCSer.Log.Trace.Console(error.ToString());
         }
     }
     return(isFieStream = false);
 }
Beispiel #6
0
        /// <summary>
        /// 释放资源
        /// </summary>
        public void Dispose()
        {
            isDisposed = true;
            if (Interlocked.CompareExchange(ref isPushFiles, 0, 1) == 1)
            {
                Files.PopNotNull(this);
                Flush();
                AutoCSer.DomainUnload.Unloader.RemoveLast(this, DomainUnload.Type.LogFileDispose, false);
                if (Pub.Log != this || AutoCSer.DomainUnload.Unloader.State != DomainUnload.State.Run)
                {
#if XAMARIN
                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                        fileStream = null;
                    }
#else
                    if (fileStream != null)
                    {
                        if (fileStream.NewLength == 0)
                        {
                            fileStream.Dispose();
                            fileStream = null;
                        }
                        else
                        {
                            string fileName = fileStream.FileName;
                            fileStream.Dispose();
                            fileStream = null;
                            AutoCSer.IO.File.MoveBak(fileName);
                        }
                    }
#endif
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// 日志写入
        /// </summary>
        private void output()
        {
            DebugInfo        value = null;
            FileStreamWriter fileStream;

            do
            {
                while (System.Threading.Interlocked.CompareExchange(ref debugLock, 1, 0) != 0)
                {
                    AutoCSer.Threading.ThreadYield.Yield(AutoCSer.Threading.ThreadYield.Type.FileLogExchangeDebug);
                }
                if (newDebugs.IsEmpty)
                {
                    System.Threading.Interlocked.Exchange(ref debugLock, 0);
                    return;
                }
                debugs.Exchange(ref newDebugs);
                System.Threading.Interlocked.Exchange(ref debugLock, 0);
                do
                {
                    try
                    {
                        while ((value = debugs.PopOnly()) != null)
                        {
                            switch (value.CacheType)
                            {
                            case CacheType.Queue:
                                HashString cacheKey = value.ToString();
                                if (cache.Get(ref cacheKey, false))
                                {
                                    fileStream = null;
                                }
                                else
                                {
                                    cache.UnsafeAdd(ref cacheKey, true);
                                    if (cache.Count > maxCacheCount)
                                    {
                                        cache.UnsafePopNode();
                                    }
                                    fileStream = this.fileStream;
                                }
                                break;

                            case CacheType.Last:
                                HashString key = value.ToString();
                                if (key.Equals(ref lastCache))
                                {
                                    fileStream = null;
                                }
                                else
                                {
                                    lastCache  = key;
                                    fileStream = this.fileStream;
                                }
                                break;

                            default: fileStream = this.fileStream; break;
                            }
                            if (fileStream != null)
                            {
                                if (value.Type == LogType.Flush)
                                {
                                    fileStream.Flush();
                                }
                                else
                                {
                                    string log = @"
" + Date.NowTime.Now.toString() + " : " + value.ToString() + @"
";
#if XAMARIN
                                    Trace.Console(log);
                                    if (fileStream.WriteNotEmpty(log) != -1 && (value.Type & LogType.Flush) != 0)
                                    {
                                        fileStream.Flush();
                                    }
#else
                                    if (fileStream.WriteNotEmpty(log) >= MaxSize && MaxSize > 0)
                                    {
                                        moveBak();
                                    }
                                    else if ((value.Type & LogType.Flush) != 0)
                                    {
                                        fileStream.Flush();
                                    }
#endif
                                }
                            }
                            pulseWait(value);
                        }
                        goto END;
                    }
                    catch
                    {
                        pulseWait(value);
                        CatchCount.Add(CatchCount.Type.Log_Output);
                    }
                }while (true);
END:
                debugs.SetHeaderNextNull();
            }while (true);
        }