代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
错误处理函数,用于记录错误日志
作者:
Dezai.CN
/ 发布于
2011/3/2
/
535
<blockquote> <div><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 错误处理函数,用于记录错误日志 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">class</span><span style="color: #000000"> SystemError { </span><span style="color: #008000">//</span><span style="color: #008000">记录错误日志位置</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">private</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">string</span><span style="color: #000000"> m_fileName </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Server.MapPath(</span><span style="color: #800000">"</span><span style="color: #800000">Systemlog.log</span><span style="color: #800000">"</span><span style="color: #000000">); </span><span style="color: #0000ff">#region</span><span style="color: #000000"> 可在web.config 中配置</span><span style="color: #000000"> </span><span style="color: #008000">//</span><span style="color: #008000"> private static string m_fileName = System.Web.HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["LogfilePathName"]); </span><span style="color: #008000">//</span><span style="color: #008000"> private string m_fileName = "c:\\Systemlog.txt"; </span><span style="color: #008000">//</span><span style="color: #008000"> <configuration> </span><span style="color: #008000">//</span><span style="color: #008000"> <appSettings> </span><span style="color: #008000">//</span><span style="color: #008000"> <add key="LogfilePathName" value="~/log/error.log"/> </span><span style="color: #008000">//</span><span style="color: #008000"> </appSettings> </span><span style="color: #008000">//</span><span style="color: #008000"> <connectionStrings/></span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">#endregion</span><span style="color: #000000"> </span><span style="color: #008000">//</span><span style="color: #008000">OleDbHelper OHelper = new OleDbHelper(); </span><span style="color: #008000">//</span><span style="color: #008000">OleDbHelper.CreateErrorLog("this is a test for error log!");</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> String FileName { </span><span style="color: #0000ff">get</span><span style="color: #000000"> { </span><span style="color: #0000ff">return</span><span style="color: #000000"> (m_fileName); } </span><span style="color: #0000ff">set</span><span style="color: #000000"> { </span><span style="color: #0000ff">if</span><span style="color: #000000"> (value </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #0000ff">null</span><span style="color: #000000"> </span><span style="color: #000000">||</span><span style="color: #000000"> value </span><span style="color: #000000">!=</span><span style="color: #000000"> </span><span style="color: #800000">""</span><span style="color: #000000">) { m_fileName </span><span style="color: #000000">=</span><span style="color: #000000"> value; } } } </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> 记录日志至文本文件 </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"></summary></span><span style="color: #008000"> </span><span style="color: #808080">///</span><span style="color: #008000"> </span><span style="color: #808080"><param name="message"></span><span style="color: #008000">记录的内容</span><span style="color: #808080"></param></span><span style="color: #808080"> </span><span style="color: #000000"> </span><span style="color: #0000ff">public</span><span style="color: #000000"> </span><span style="color: #0000ff">static</span><span style="color: #000000"> </span><span style="color: #0000ff">void</span><span style="color: #000000"> CreateErrorLog(</span><span style="color: #0000ff">string</span><span style="color: #000000"> message) { String MyURLA, MyURLB, MyURLC, MyURLD, MyURLE, MyURLF; MyURLA </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.RawUrl.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000"> /web/CompanyNews.aspx?ID=6</span><span style="color: #008000"> </span><span style="color: #000000"> MyURLB </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000"> /web/CompanyNews.aspx 无参数</span><span style="color: #008000"> </span><span style="color: #000000"> MyURLC </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.Url.PathAndQuery.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000"> 同 Request.RawUrl</span><span style="color: #008000"> </span><span style="color: #000000"> MyURLD </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.CurrentExecutionFilePath.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000"> /web/CompanyNews.aspx 同 Request.Url.AbsolutePath</span><span style="color: #008000"> </span><span style="color: #000000"> MyURLE </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.Url.AbsoluteUri.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000"> </span><span style="color: #008000; text-decoration: underline">http://localhost</span><span style="color: #008000">:1108/web/CompanyNews.aspx?ID=6</span><span style="color: #008000"> </span><span style="color: #000000"> MyURLF </span><span style="color: #000000">=</span><span style="color: #000000"> System.Web.HttpContext.Current.Request.UserHostAddress.ToString(); </span><span style="color: #008000">//</span><span style="color: #008000">127.0.0.1</span><span style="color: #008000"> </span><span style="color: #000000"> </span><span style="color: #0000ff">if</span><span style="color: #000000"> (File.Exists(m_fileName)) { </span><span style="color: #808080">///</span><span style="color: #008000">如果日志文件已经存在,则直接写入日志文件</span><span style="color: #808080"> </span><span style="color: #000000"> StreamWriter sr </span><span style="color: #000000">=</span><span style="color: #000000"> File.AppendText(FileName); sr.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">\n</span><span style="color: #800000">"</span><span style="color: #000000">); sr.WriteLine(</span><span style="color: #800000">"</span><span style="color: #800000">*</span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> DateTime.Now.ToString() </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"> | </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> MyURLA </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #800000">"</span><span style="color: #800000"> | </span><span style="color: #800000">"</span><span style="color: #000000"> </span><span style="color: #000000">+</span><span style="color: #000000"> message); </span><span style="color: #008000">//</span><span style="color: #008000">sr.WriteLine("\n"); </span><span style="color: #008000">//</span><span style="color: #008000">sr.WriteLine("*" + DateTime.Now.ToString() + " | " + MyURLD + " | " + message);</span><span style="color: #008000"> </span><span style="color: #000000"> sr.Close(); } </span><span style="color: #0000ff">else</span><span style="color: #000000"> { </span><span style="color: #808080">///</span><span style="color: #008000">创建日志文件</span><span style="color: #808080"> </span><span style="color: #000000"> StreamWriter sr </span><span style="color: #000000">=</span><span style="color: #000000"> File.CreateText(FileName); sr.Close(); } } }</span> </div> </blockquote>
试试其它关键字
错误处理
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3