• .NET代码错误日志设计


     

     

     

    1.首先创建一个类Files

    using System.IO;

    using System.Security.AccessControl;

    namespace 命名空间

    {

        public class Files

        {

            /// <summary>

            /// 给指定的操作系统用户赋操作权限

            /// </summary>

            /// <param name="pathname">文件的路径</param>

            /// <param name="username">操作系统用户名</param>

            /// <param name="power">操作权限,枚举型:FullControl,ReadOnly,Write,Modify</param>

            /// <returns>是否成功</returns>

            public static bool addpathPower(string pathname, string username, string power)

            {

                bool istrue = true;

                DirectoryInfo dirinfo = new DirectoryInfo(pathname);

     

                if ((dirinfo.Attributes & FileAttributes.ReadOnly) != 0)

                {

                    dirinfo.Attributes = FileAttributes.Normal;

                }

                //取得访问控制列表  

                DirectorySecurity dirsecurity = dirinfo.GetAccessControl();

     

                switch (power)

                {

                    case "FullControl":

                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.InheritOnly, AccessControlType.Allow));

                        break;

                    case "ReadOnly":

                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Read, AccessControlType.Allow));

                        break;

                    case "Write":

                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Write, AccessControlType.Allow));

                        break;

                    case "Modify":

                        dirsecurity.AddAccessRule(new FileSystemAccessRule(username, FileSystemRights.Modify, AccessControlType.Allow));

                        break;

                }

                try

                {

                    dirinfo.SetAccessControl(dirsecurity);

                }

                catch

                {

                    istrue = false;

                }

                return istrue;

            }

        }

    }

    2.之后创建Log类

    using System.Text;

    using System.IO;

     

    namespace命名空间

    {

        public class Log

        {

            #region  记录错误日志

            /// <summary>

            /// 记录错误日志

            /// </summary>

            /// <param name="strformat">错误信息</param>

            /// <param name="path">日志存放位置</param>

            public static void WriteErrorLog(string strformat, string path)

            {

                try

                {

                    if (!File.Exists(path))

                    {

                        string nofile = Path.GetDirectoryName(path);

                        if (!Directory.Exists(nofile))

                        {

                            Directory.CreateDirectory(nofile);

                            Files.addpathPower(nofile, "ASPNET", "FullControl");

                        }

                        FileStream fss = File.Create(path); ;

                        fss.Flush();

                        fss.Close();//创建之后进行关闭 

     

                    }

     

                    FileStream fs = new FileStream(path, FileMode.Append);

                    StreamWriter streamWriter = new StreamWriter(fs);

                    streamWriter.Write(DateTime.Now.ToString() + ":" + strformat + " " + "-------------------------- ");

                    streamWriter.Flush();

                    streamWriter.Close();

                    fs.Close();

                }

                catch

                {

     

                }

            }

     

            /// <summary>

            /// 记录错误日志

            /// </summary>

            /// <param name="strformat">错误信息,和发送异常位置,系统名</param>

            /// <param name="fuzeren">负责人</param>

            public static void WriteError(string strformat, string fuzeren)

            {

                try

                {

                    WriteErrorLog("负责人:" + fuzeren + strformat, "c://Program Files//cutt//error.txt");

                    string path = "c://Program Files//cutt//" + fuzeren + "_error.txt";

                    if (!File.Exists(path))

                    {

                        string nofile = Path.GetDirectoryName(path);

                        if (!Directory.Exists(nofile))

                        {

                            Directory.CreateDirectory(nofile);

                            Files.addpathPower(nofile, "ASPNET", "FullControl"); //给aspnet用户加权限,避免权限不够不能写入服务器文件

                        }

                        FileStream fss =File.Create(path);;

                        fss.Flush();

                        fss.Close();//创建之后进行关闭 ,否则直接写入报错 

                    }

                    FileStream fs = new FileStream(path, FileMode.Append);

                    StreamWriter streamWriter = new StreamWriter(fs);

                    streamWriter.Write(DateTime.Now.ToString() + ":" + strformat + " " + "-------------------------- ");

                    streamWriter.Flush();

                    streamWriter.Close();

                    fs.Close();

                }

                catch

                {

     

                }

     

            }

           

            #endregion

        }

    }

  • 相关阅读:
    splay复杂度的证明
    splay的写法
    洛谷 P3722 [AH2017/HNOI2017]影魔
    洛谷 P4770 [NOI2018]你的名字
    清北考前刷题day3下午好
    P3043 [USACO12JAN]牛联盟Bovine Alliance(并查集)
    bzoj3252攻略(线段树+dfs序)
    清北考前刷题day2早安
    清北考前刷题day2下午好
    清北考前刷题day1下午好
  • 原文地址:https://www.cnblogs.com/pancong/p/3595990.html
Copyright © 2020-2023  润新知