using System;
using System.Collections.Generic;
using System.Threading; public class LogManager
{
/// <summary>
/// 构造函数
/// </summary>
static LogManager ()
{
Start();
} #region 队列方法 /// <summary>
/// 日志队列
/// </summary>
private static Queue<Log> ListQueue = new Queue<Log>(); class Log
{
public string File { get; set; }
public string Msg { get; set; }
} public static void WriteLog(string logFile, string msg)
{
Log log = new Log()
{
File = logFile,
Msg = msg
}; ListQueue.Enqueue(log);
} private static void Start()//启动
{ WriteLog("ULog", "Start");
Thread thread = new Thread(threadStart);
thread.IsBackground = true;
thread.Start(); } private static void threadStart()
{
while (true)
{
if (ListQueue.Count > 0)
{
try
{
ScanQueue();
}
catch (Exception ex)
{
throw;
//LO_LogInfo.WLlog(ex.ToString());
}
}
else
{
//没有任务,休息3秒钟
Thread.Sleep(1000);
}
}
}
//要执行的方法
private static void ScanQueue()
{
while (ListQueue.Count > 0)
{
try
{
//从队列中取出
Log log = ListQueue.Dequeue();
ThreadLog(log.File, log.Msg); //Console.WriteLine(queueinfo.feedid);
//取出的queueinfo就可以用了,里面有你要的东西
//以下就是处理程序了
//。。。。。。 }
catch (Exception ex)
{
throw;
}
}
} #endregion private static string logPath = string.Empty; /// <summary>
/// 保存日志的文件夹
/// </summary>
public static string LogPath
{
get
{
if (logPath == string.Empty)
{
if (System.Web.HttpContext.Current == null)
// Windows Forms 应用
logPath = AppDomain.CurrentDomain.BaseDirectory + @"Logs\";
else
// Web 应用
logPath = AppDomain.CurrentDomain.BaseDirectory + @"Logs\";
}
return logPath;
}
set { logPath = value; }
} private static string logFielPrefix = string.Empty;
/// <summary>
/// 日志文件前缀
/// </summary>
public static string LogFielPrefix
{
get { return logFielPrefix; }
set { logFielPrefix = value; }
} /// <summary>
/// 写日志
/// </summary>
private static void ThreadLog(string logFile, string msg)
{
try
{
System.IO.StreamWriter sw = System.IO.File.AppendText(
LogPath + DateTime.Now.ToString("yyyyMMdd") +
LogFielPrefix + " " + logFile + ".Log" );
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff : ") + msg);
sw.Close();
}
catch
{ }
} /// <summary>
/// 写日志
/// </summary>
public static void WriteLog(LogFile logFile, string msg)
{
WriteLog(logFile.ToString(), msg);
}
} /// <summary>
/// 日志类型
/// </summary>
public enum LogFile
{
Trace,
Warning,
Error,
SQL
}

  

c# 多线程使用队列顺序写日志的类 (需要再优化)的更多相关文章

  1. Delphi 写日志的类

    unit uProgLog; interface uses Windows, SysUtils, SyncObjs; const C_LOG_LEVEL_TRACE = $; C_LOG_LEVEL_ ...

  2. C#写日志工具类

    代码: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System ...

  3. 在单进程单线程或单进程多线程下实现log4cplus写日志并按大小切割

    基于脚本配置来过滤log信息 除了通过程序实现对log环境的配置之外.log4cplus通过PropertyConfigurator类实现了基于脚本配置的功能.通过 脚本能够完毕对logger.app ...

  4. PHP写日志公共类

    Txl_Log.php <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * * * ...

  5. C# 超高速高性能写日志 代码开源

    1.需求 需求很简单,就是在C#开发中高速写日志.比如在高并发,高流量的地方需要写日志.我们知道程序在操作磁盘时是比较耗时的,所以我们把日志写到磁盘上会有一定的时间耗在上面,这些并不是我们想看到的. ...

  6. [转]C# 超高速高性能写日志 代码开源

      1.需求 需求很简单,就是在C#开发中高速写日志.比如在高并发,高流量的地方需要写日志.我们知道程序在操作磁盘时是比较耗时的,所以我们把日志写到磁盘上会有一定的时间耗在上面,这些并不是我们想看到的 ...

  7. 写日志(log)

    已下为我自己写的一个写日志的类,比较简洁. <?php class Log { /** * @Purpose : 写日志 * @Method Name : writeLog() * @param ...

  8. C#写文本日志帮助类(支持多线程)

    代码: using System; using System.Configuration; using System.IO; using System.Threading.Tasks; namespa ...

  9. 磁盘IO单线程顺序写时最快的,如果多线程写,磁盘的磁头要不断重新寻址,所以写入速度反而会慢

    (1) 读写最好还是不要多线程,硬盘读写的速度有限,单线程时已经满负荷了,多线程又会增加线程之间的切换,会增加时间. 如果想增加读写速度,应该增加硬盘,做raid (2)首先是硬盘的写入是串行的,CP ...

随机推荐

  1. Linux服务器操作

    Linux关于服务器的操作(root权限) 1. 查看服务器版本信息 cat /etc/redhat-release 2.将Windows上文件通过xshell传到Linux服务器 查找系统自带软件包 ...

  2. TCP拥塞控制算法

    转自浅谈TCP拥塞控制算法 本篇文章介绍了几种经典的TCP拥塞控制算法,包括算法原理及各自适用场景. 回顾上篇文章:浅谈 redis 延迟 前言 TCP 通过维护一个拥塞窗口来进行拥塞控制,拥塞控制的 ...

  3. python 文件压缩及解压

    文件压缩 import os import zipfile def zip_dir(dirname,zipfilename): """ | ##@函数目的: 压缩指定目录 ...

  4. vue 动态渲染数据很慢或不渲染

    vue 动态渲染数据很慢或不渲染 原因是因为vue检测速度很慢,因为多层循环了,在VUE 2.x的时候还能渲染出来,1.x的时候压根渲染不出来.解决方式:在动态改变数据的方法,第一行加上 this.$ ...

  5. svg简单的应用

    1.可以直接在html内写svg (1)width宽度,height高度 (2)xmlns svg的规则 <svg xmlns="http://www.w3.org/2000/svg& ...

  6. python —— 文本特征提取 CountVectorize

    CountVectorize 来自:python学习 文本特征提取(二) CountVectorizer TfidfVectorizer 中文处理 - CSDN博客 https://blog.csdn ...

  7. RabbitMQ学习之:(九)Headers Exchange (转贴+我的评论)

    From: http://lostechies.com/derekgreer/2012/05/29/rabbitmq-for-windows-headers-exchanges/ RabbitMQ f ...

  8. java.lang.reflect.Method.getAnnotation()方法示例【通过反射获取到方法对象再获取方法对象上的注解信息】

    转: java.lang.reflect.Method.getAnnotation()方法示例 java.lang.reflect.Method.getAnnotation(Class <T&g ...

  9. Linux 查看 MySQL的版本信息

    Linux 查看 MySQL的版本信息 如何查看某台 Linux 主机上的 安装的 MySQL版本信息? 使用如下命令,查看 MySQL的版本信息: mysql -V 或者 mysql --versi ...

  10. Servlet(2):Requset/Response Encoding and Filter

    Requset/Response Encoding 表单提交分GET和POST,接下来分开讨论. (1)GET/URL提交的数据 在 Tomcat中,默认情况下使用"URIEncoding& ...