码迷,mamicode.com
首页 > 其他好文 > 详细

工具类:Log

时间:2015-01-27 10:45:09      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

public static string LogPath
{
    get
    {
        string path = string.Empty;
        path = Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFiles) +
                @"\Microsoft Shared\web server extensions\12\LOGS\";
        if (!Directory.Exists(path))
        {
            path = @"c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\LOGS\";
        }
        return path;
    }
}
 
public static void Write(string Message)
{
    FileStream FS = null;
    StreamWriter SW = null;
    try
    {
        SPSecurity.RunWithElevatedPrivileges
            (
                delegate
                {
                    FS = File.Open(
                        string.Format(
                        "{0}PosterscopeUSA Notification-{1}-{2}-{3}.log",
                        LogPath, 
                        DateTime.Now.Year,
                        DateTime.Now.Month,
                        DateTime.Now.Day),
                        FileMode.Append);
                    SW = new StreamWriter(FS);
                    SW.Write(string.Format("{0}\r\n{1}\r\n\r\n", DateTime.Now, Message));
                    SW.Close();
                    SW.Dispose();
                    FS.Close();
                    FS.Dispose();
                }
            );
    }
    catch { }
    finally
    {
        SW = null;
        FS = null;
    }
}

  

工具类:Log

标签:

原文地址:http://www.cnblogs.com/batter152/p/4244012.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!