Example #1
0
        public static bool Write(string logContent, string fileName)
        {
            bool result = true;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = "AppError";
            }
            try
            {
                string   text     = Path.Combine(TextLogger.APP_LOG_DIRECTORY, DateTime.Now.ToString("yyyyMMdd") + fileName + TextLogger.LOG_SUFFIX);
                FileInfo fileInfo = new FileInfo(text);
                if (fileInfo.Exists && fileInfo.Length >= 800000L)
                {
                    fileInfo.CopyTo(text.Replace(TextLogger.LOG_SUFFIX, TextUtility.CreateRandomNum(5) + TextLogger.LOG_SUFFIX));
                    File.Delete(text);
                }
                FileStream    fileStream    = new FileStream(text, FileMode.Append, FileAccess.Write, FileShare.Read);
                StreamWriter  streamWriter  = new StreamWriter(fileStream, Encoding.UTF8);
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendFormat("{0:yyyy'/'MM'/'dd' 'HH':'mm':'ss}", DateTime.Now);
                stringBuilder.Append("|");
                if (TextLogger.WRITE_APP_LOG)
                {
                    stringBuilder.Append(logContent.Replace("\r", "").Replace("\n", "<br />"));
                    stringBuilder.Append("|");
                    stringBuilder.Append(GameRequest.GetUserIP());
                    stringBuilder.Append("|");
                    stringBuilder.Append(GameRequest.GetUrl());
                }
                else
                {
                    stringBuilder.Append(logContent);
                }
                stringBuilder.Append("\r\n");
                streamWriter.Write(stringBuilder.ToString());
                streamWriter.Flush();
                streamWriter.Close();
                streamWriter.Dispose();
                fileStream.Close();
                fileStream.Dispose();
            }
            catch
            {
                result = false;
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// 写日志文件
        /// </summary>
        /// <param name="logContent">日志内容</param>
        /// <param name="fileName">日志文件名</param>
        /// <returns>返回值true成功,false失败</returns>
        public static bool Write(string logContent, string fileName)
        {
            bool flag = true;

            if (string.IsNullOrEmpty(fileName))
            {
                fileName = "AppError";
            }
            try
            {
                string   str  = Path.Combine(APP_LOG_DIRECTORY, DateTime.Now.ToString("yyyyMMdd") + fileName + LOG_SUFFIX);
                FileInfo info = new FileInfo(str);
                if (info.Exists && (info.Length >= 0xc3500L))
                {
                    info.CopyTo(str.Replace(LOG_SUFFIX, TextUtility.CreateRandomNum(5) + LOG_SUFFIX));
                    File.Delete(str);
                }
                FileStream    stream  = new FileStream(str, FileMode.Append, FileAccess.Write, FileShare.Read);
                StreamWriter  writer  = new StreamWriter(stream, Encoding.UTF8);
                StringBuilder builder = new StringBuilder();
                builder.AppendFormat("{0:yyyy'/'MM'/'dd' 'HH':'mm':'ss}", DateTime.Now);
                builder.Append("|");
                if (WRITE_APP_LOG)
                {
                    builder.Append(logContent.Replace("\r", "").Replace("\n", "<br />"));
                    builder.Append("|");
                    builder.Append(GameRequest.GetUserIP());
                    builder.Append("|");
                    builder.Append(GameRequest.GetUrl());
                }
                else
                {
                    builder.Append(logContent);
                }
                builder.Append("\r\n");
                writer.Write(builder.ToString());
                writer.Flush();
                writer.Close();
                writer.Dispose();
                stream.Close();
                stream.Dispose();
            }
            catch
            {
                flag = false;
            }
            return(flag);
        }