输出日志信息到文本中
using System;
using System.Configuration;
namespace Common
{
//配置Config文件信息
public class Config
{
/// <summary>
/// 数据库字符串
/// </summary>
public static string SQLConnString
{
get
{
return GetAppSettingToString("SQLDB");
}
}
/// <summary>
/// 是否启用调式日志
/// </summary>
public static bool IsDebugLog
{
get
{
return GetConfigBool("DebugLog");
}
}
public static bool IsUploadByDate
{
get
{
return GetConfigBool("IsUploadByDate");
}
}
/// <summary>
/// 调试日志路径
/// </summary>
public static string LoggerPath
{
get
{
return GetAppSettingToString("LoggerPath");
//return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
}
/// <summary>
/// 获取AppSetting节点下的配置项
/// </summary>
/// <param name="name">节点名称</param>
/// <param name="_default">默认值</param>
/// <returns>对应节点的值</returns>
public static string GetAppSettingToString(string name, string _default = "")
{
string sTmp = _default;
try
{
sTmp = System.Configuration.ConfigurationManager.AppSettings[name] ?? _default;
}
catch
{
sTmp = _default;
}
return sTmp;
}
/// <summary>
/// 修改AppSettings中配置
/// </summary>
/// <param name="key">key值</param>
/// <param name="value">相应值</param>
public static bool SetAppSettingValue(string key, string value)
{
try
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] != null)
{
config.AppSettings.Settings[key].Value = value;
}
else
{
config.AppSettings.Settings.Add(key, value);
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch (Exception ex)
{
string message = string.Format("修改AppSettings异常:{0}", ex.ToString());
CommonData.WriteErrorLog(message);
return false;
}
}
/// <summary>
/// 得到AppSettings中的配置Bool信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
bool result = false;
string cfgVal = GetAppSettingToString(key);
if (null != cfgVal && string.Empty != cfgVal)
{
try
{
result = bool.Parse(cfgVal);
}
catch (FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置Decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static decimal GetConfigDecimal(string key, decimal _default = 0)
{
decimal result = 0;
string cfgVal = GetAppSettingToString(key);
if (null != cfgVal && string.Empty != cfgVal)
{
try
{
result = decimal.Parse(cfgVal);
}
catch (FormatException)
{
result = _default;
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置int信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetConfigInt(string key, int _default = 0)
{
int result = 0;
string cfgVal = GetAppSettingToString(key);
if (null != cfgVal && string.Empty != cfgVal)
{
try
{
result = int.Parse(cfgVal);
}
catch (FormatException)
{
result = _default;
}
}
return result;
}
}
}
/****************************************************************
* 描述:文本日志操作
*****************************************************************/
using System;
using System.IO;
using System.Text;
namespace Common
{
/// <summary>
/// 文本日志
/// </summary>
public class TextLog
{
private static TextLog textLog;
private string logFilePath;
private TextLog()
{
this.logFilePath = this.GetLogPath();
}
public void WriteLog(LogLevel level, string information)
{
FileStream fs = null;
object obj = new object();
StringBuilder sb = new StringBuilder();
lock (obj)
{
try
{
this.logFilePath = this.GetLogPath();
using (fs = new FileStream(logFilePath, FileMode.Append))
{
sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
switch (level)
{
case LogLevel.Info:
default:
sb.Append(" 信息:");
break;
case LogLevel.Error:
sb.Append(" 错误:");
break;
case LogLevel.Warring:
sb.Append(" 警告:");
break;
}
sb.Append("
");
sb.Append(information);
sb.Append("
");
byte[] data = Encoding.Default.GetBytes(sb.ToString());
fs.Write(data, 0, data.Length);
}
}
catch { }
}
}
private string GetLogPath()
{
// string path = Directory.GetCurrentDirectory() ;
DateTime date = DateTime.Now;
string path = string.Format(@"{0}{1}{2}.log", Config.LoggerPath, date.ToString("yyyy-MM"), date.Day.ToString());
if (!Directory.Exists(Path.GetDirectoryName(path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
return path;
}
public static TextLog GetInstance()
{
object obj = new object();
if (textLog == null)
{
lock (obj)
{
try
{
textLog = new TextLog();
}
catch { }
}
}
return textLog;
//textLog = new TextLog();
//return textLog;
}
}
public enum LogLevel
{
/// <summary>
/// 信息
/// </summary>
Info = 1,
/// <summary>
/// 错误
/// </summary>
Error = 2,
/// <summary>
/// 警告
/// </summary>
Warring = 3
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Common
{
public class CommonData
{
#region 写入日志操作
/// <summary>
/// 写入信息日志
/// </summary>
/// <param name="msg"></param>
public static void WriteInfoLog(string msg)
{
TextLog.GetInstance().WriteLog(LogLevel.Info, msg);
}
/// <summary>
/// 写入错误日志
/// </summary>
/// <param name="msg"></param>
public static void WriteErrorLog(string msg)
{
TextLog.GetInstance().WriteLog(LogLevel.Error, msg);
}
/// <summary>
/// 写入警告日志
/// </summary>
/// <param name="msg"></param>
public static void WriteWwarringLog(string msg)
{
TextLog.GetInstance().WriteLog(LogLevel.Warring, msg);
}
/// <summary>
/// 根据配置写入调试日志
/// </summary>
/// <param name="msg"></param>
public static void WriteDebugLog(string msg)
{
if (Config.IsDebugLog)
{
TextLog.GetInstance().WriteLog(LogLevel.Info,string.Format("调式日志:{0}",msg));
}
}
#endregion
}
}