原创

C#-工具-JSLogs-自定义简单日志工具-Error基本邮件通知

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Web;
using System.Security.AccessControl;

namespace WebHelper
{
    /// <summary>
    /// 自定义简单日志工具 !!! 根据错误级别做不同的上报处理
    /// </summary>
    public class JSLogs
    {
        public static void Error(string Content)
        {

            DateTime now = System.DateTime.Now;

            String path = HttpContext.Current.Server.MapPath("~/Logs/Error/");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            String fileUrl = Path.Combine(path, "error-" + now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xml"); // 特别注意 文件命名不要包含英文下的冒号:(System.NotSupportedException),中文的冒号可以  

            write(fileUrl, now, Content, true, 0);// 必须邮件通知
        }

        public static void Warn(string Content)
        {

            DateTime now = System.DateTime.Now;

            String path = HttpContext.Current.Server.MapPath("~/Logs/Warn");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            String fileUrl = Path.Combine(path, "warn-" + now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xml");
            write(fileUrl, now, Content, true, 1);// 暂时必须文件通知  后期视请求取消
        }

        public static void Warn(string Content, bool IsSendEmail)
        {

            DateTime now = System.DateTime.Now;

            String path = HttpContext.Current.Server.MapPath("~/Logs/Warn");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            String fileUrl = Path.Combine(path, "warn-" + now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xml");
            write(fileUrl, now, Content, IsSendEmail, 1);// 暂时必须文件通知  后期视请求取消
        }

        public static void Info(string Content)
        {

            DateTime now = System.DateTime.Now;

            String path = HttpContext.Current.Server.MapPath("~/Logs/Info");
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }

            String fileUrl = Path.Combine(path, "info-" + now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xml"); ;
            write(fileUrl, now, Content, false, -1);// 不发送邮件
        }
        private static void write(String FileUrl, DateTime Now, String Content, bool isSendEmail, int type)
        {
            if (isSendEmail)
            {
                sendEmail(type, Content);// 发送邮件
            }
            XmlDocument xmldoc = new XmlDocument();
            try
            {

                if (File.Exists(FileUrl))
                {
                    //根据模板创建文件
                    xmldoc.Load(FileUrl);
                }
                else
                {
                    //加入XML的声明段落
                    XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                    xmldoc.AppendChild(xmlnode);

                    //加入一个根元素                    
                    XmlElement xmlelem = xmldoc.CreateElement("", "ROOT", "");
                    xmldoc.AppendChild(xmlelem);
                }

                String ParentNodeName = "UnKnow";
                if (type == 0)
                {
                    ParentNodeName = "Error";
                }
                else if (type == 1)
                {
                    ParentNodeName = "Warn";
                }
                else if (type == 2)
                {
                    ParentNodeName = "Info";
                }

                //添加父节点
                XmlElement elemError = xmldoc.CreateElement("", ParentNodeName, "");
                xmldoc.ChildNodes.Item(1).AppendChild(elemError);

                //添加错误信息
                XmlElement elemMsg = xmldoc.CreateElement("", "MSG", "");
                XmlText textMsg = xmldoc.CreateTextNode(Content);
                elemMsg.AppendChild(textMsg);
                elemError.AppendChild(elemMsg);

                //添加时间
                XmlElement elemTime = xmldoc.CreateElement("", "TIME", "");
                XmlText textTime = xmldoc.CreateTextNode(Now.ToString());
                elemTime.AppendChild(textTime);
                elemError.AppendChild(elemTime);


                if (!File.Exists(FileUrl))
                {
                    FileStream fs = File.Create(FileUrl);
                    fs.Close();//必须加上 否则提示文件被占用
                }

                xmldoc.Save(FileUrl);
            }
            catch (Exception ex)// 注意 这里写入到本地日志失败,未确定是不是访问权限的问题
            { //发个邮件出去
                //  throw new ApplicationException("Utility.Error.WriteErrorXml:" + ex.Message);
                sendEmail(3, ex.ToString());// 发送邮件
            }
        }


        private static void sendEmail(int type, String Content)
        {
            if (type == 0)
            {
                EmailSender.SendEmailMsg("微信公众号项目-Error日志", Content);
            }
            else if (type == 1)
            {
                EmailSender.SendEmailMsg("微信公众号项目-Warn日志", Content);
            }
            else if (type == 2)
            {
                EmailSender.SendEmailMsg("微信公众号项目-Info日志", Content);
            }
            else if (type == 3)
            {
                EmailSender.SendEmailMsg("微信公众号项目-JSLogs.cs工具内部错误", Content);
            }

        }
        /// <summary>
        /// 为文件添加users,everyone用户组的完全控制权限
        /// </summary>
        /// <param name="filePath"></param>
        static void AddSecurityControll2File(string filePath)
        {

            //获取文件信息
            FileInfo fileInfo = new FileInfo(filePath);
            //获得该文件的访问权限
            System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
            //添加ereryone用户组的访问权限规则 完全控制权限
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
            //添加Users用户组的访问权限规则 完全控制权限
            fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
            //设置访问权限
            fileInfo.SetAccessControl(fileSecurity);
        }

        /// <summary>
        ///为文件夹添加users,everyone用户组的完全控制权限 好像不能嵌套 嵌套的话需要 递归处理
        /// </summary>
        /// <param name="dirPath"></param>
        static void AddSecurityControll2Folder(string dirPath)
        {
            //获取文件夹信息
            DirectoryInfo dir = new DirectoryInfo(dirPath);
            //获得该文件夹的所有访问权限
            System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
            //设定文件ACL继承
            InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
            //添加ereryone用户组的访问权限规则 完全控制权限
            FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            //添加Users用户组的访问权限规则 完全控制权限
            FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
            bool isModified = false;
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
            dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
            //设置访问权限
            dir.SetAccessControl(dirSecurity);
        }

    }
}
正文到此结束
本文目录