Created
December 10, 2014 01:05
-
-
Save liulixiang1988/caf3a05d16fbafbf2c5f to your computer and use it in GitHub Desktop.
C# Log.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.IO; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace Liulx.Util | |
{ | |
public class Log | |
{ | |
private static ConcurrentQueue<string> _logQueue = new ConcurrentQueue<string>(); | |
private static ConcurrentQueue<string> _errorQueue = new ConcurrentQueue<string>(); | |
private static readonly Task _logTask = new Task(CreateNormalLog); | |
private static readonly Task _errorLogTask = new Task(CreateErrorLog); | |
public static void BeginLog() | |
{ | |
_logTask.Start(); | |
_errorLogTask.Start(); | |
} | |
public static void EndLog() | |
{ | |
_isListen = false; | |
} | |
private static bool _isListen = true; | |
private static void CreateNormalLog() | |
{ | |
while (_isListen) | |
{ | |
if (_logQueue == null) | |
{ | |
_logQueue = new ConcurrentQueue<string>(); | |
} | |
for (int i = 0; i < _logQueue.Count; i++) | |
{ | |
string line; | |
var b = _logQueue.TryDequeue(out line); | |
if (b) | |
{ | |
CreateMsg(NormalLogFileName, line); | |
} | |
} | |
Thread.Sleep(1000); | |
} | |
} | |
private static void CreateErrorLog() | |
{ | |
while (_isListen) | |
{ | |
if (_errorQueue == null) | |
{ | |
_errorQueue = new ConcurrentQueue<string>(); | |
} | |
for (int i = 0; i < _errorQueue.Count; i++) | |
{ | |
string line; | |
var b = _errorQueue.TryDequeue(out line); | |
if (b) | |
{ | |
CreateMsg(ErrorLogFileName, line); | |
} | |
} | |
Thread.Sleep(1000); | |
} | |
} | |
public static String ErrorLogFileName | |
{ | |
get | |
{ | |
return Path.Combine(Config.Default.LogFolder, DateTime.Today.ToString("yyy-MM-dd") + "Error.txt"); | |
} | |
} | |
/// <summary> | |
/// 正常日志 | |
/// </summary> | |
public static String NormalLogFileName | |
{ | |
get | |
{ | |
return Path.Combine(Config.Default.LogFolder, DateTime.Today.ToString("yyy-MM-dd") + "Log.txt"); | |
} | |
} | |
public static void CreateMsg(string fileName, params string[] messages) | |
{ | |
try | |
{ | |
var info = messages.Aggregate("", (current, message) => current + (message + "\t")); | |
if (File.Exists(fileName)) | |
{ | |
//如果日志文件已经存在,则直接写入日志文件 | |
StreamWriter sr = File.AppendText(fileName); | |
sr.WriteLine(DateTime.Now + "---" + info); | |
sr.Close(); | |
} | |
else | |
{ | |
//创建日志文件 | |
StreamWriter sr = File.CreateText(fileName); | |
sr.WriteLine(DateTime.Now + "---" + info); | |
sr.Close(); | |
} | |
} | |
catch (Exception exception) | |
{ | |
using (var db = new AppDbContext()) | |
{ | |
db.LogInfos.Add(new LogInfo | |
{ | |
CreateTime = DateTime.Now, | |
InfoType = 2, | |
Info = exception.Message | |
}); | |
db.SaveChanges(); | |
} | |
} | |
} | |
/// <summary> | |
/// 记录日志至文本文件 | |
/// </summary> | |
/// <param name="messages">记录的内容</param> | |
public static void CreateErrorMsg(params string[] messages) | |
{ | |
var info = messages.Aggregate("", (current, message) => current + (message + "\t")); | |
_errorQueue.Enqueue(info); | |
} | |
public static void CreateLogMsg(params string[] messages) | |
{ | |
var info = messages.Aggregate("", (current, message) => current + (message + "\t")); | |
_logQueue.Enqueue(info); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment