Skip to content

Instantly share code, notes, and snippets.

@lbp0200
Created February 13, 2015 08:30
Show Gist options
  • Save lbp0200/1d759410f012872736fa to your computer and use it in GitHub Desktop.
Save lbp0200/1d759410f012872736fa to your computer and use it in GitHub Desktop.
Asp.Net 带缓冲Log组件核心代码
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"));
}
}
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