Created
February 13, 2015 08:30
-
-
Save lbp0200/1d759410f012872736fa to your computer and use it in GitHub Desktop.
Asp.Net 带缓冲Log组件核心代码
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
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
AreaRegistration.RegisterAllAreas(); | |
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); | |
RouteConfig.RegisterRoutes(RouteTable.Routes); | |
BundleConfig.RegisterBundles(BundleTable.Bundles); | |
} | |
public override void Init() | |
{ | |
base.Init(); | |
this.RequestCompleted += MvcApplication_RequestCompleted; | |
} | |
void MvcApplication_RequestCompleted(object sender, EventArgs e) | |
{ | |
LogModel.Info(string.Format(DateTime.Now.ToString() + Guid.NewGuid().ToString() + "\r\n")); | |
} | |
} |
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.Text; | |
public static class LogModel | |
{ | |
static ConcurrentQueue<string> bufferQueue = new ConcurrentQueue<string>(); | |
static object lockObj = new object(); | |
public static void Info(string msg) | |
{ | |
bufferQueue.Enqueue(msg); | |
if (bufferQueue.Count > 10) | |
{ | |
lock (lockObj) | |
{ | |
string omsg = null; | |
StringBuilder sb = new StringBuilder(); | |
while (bufferQueue.TryDequeue(out omsg)) | |
{ | |
sb.Append(omsg); | |
} | |
File.AppendAllText(@"D:\LogFile.log", sb.ToString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment