示例#1
0
        /// <summary>
        /// 获取要显示的日志根目录,
        /// 重写这个方法可修改默认的根目录(FileWriter指定的目录)。
        /// </summary>
        /// <returns></returns>
        public virtual string GetLogRootDirectory()
        {
            WriterFactory.Init();

            WriterSection config = (from x in WriterFactory.Config.Writers
                                    where x.Type.StartsWith(typeof(FileWriter).FullName + ",")
                                    select x).FirstOrDefault();

            if (config == null)
            {
                WriteMessage("ClownFish.Log.config中没有配置FileWriter");
                return(null);
            }

            string path = config.GetOptionValue("RootDirectory");

            if (string.IsNullOrEmpty(path))
            {
                WriteMessage("ClownFish.Log.config中,没有为FileWriter指定RootDirectory属性。");
                return(null);
            }

            path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
            if (Directory.Exists(path) == false)
            {
                WriteMessage("ClownFish.Log.config中,为FileWriter指定RootDirectory目录不存在。");
                return(null);
            }

            return(path);
        }
示例#2
0
        public void Init(WriterSection config)
        {
            string value = config.GetOptionValue("RootPath");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为MsmqWriter指定RootPath属性。");
            }


            if (s_rootPath != null)
            {
                return;
            }


            // 检查需要记录的各个数据类型的队列是否存在。
            foreach (var item in WriterFactory.Config.Types)
            {
                string path = value + "-" + item.Type.Name;

                if (MessageQueue.Exists(path) == false)
                {
                    using (MessageQueue messageQueue = MessageQueue.Create(path)) {
                        messageQueue.SetPermissions("Everyone", MessageQueueAccessRights.FullControl);
                    }
                }
            }

            s_rootPath = value + "-";
        }
示例#3
0
        private static List <NameValue> ReadHttpArgs(WriterSection config, string flag)
        {
            List <NameValue> list = new List <NameValue>();

            foreach (var item in config.Options)
            {
                string key = item.Key;

                if (key.StartsWith(flag))
                {
                    key = key.Substring(flag.Length);
                    if (key.Trim().Length == 0)
                    {
                        continue;
                    }

                    list.Add(new NameValue {
                        Name = key, Value = item.Value
                    });
                }
            }

            if (list.Count == 0)
            {
                return(null);
            }
            else
            {
                return(list);
            }
        }
示例#4
0
        public void Test3()
        {
            WriterSection mail = new WriterSection();

            mail.Name = "Mail";
            mail.Type = "ClownFish.Log.Serializer.MailWriter, ClownFish.Log";

            MailWriter writer = new MailWriter();

            writer.Init(mail);
        }
示例#5
0
        public void Test2()
        {
            WriterSection file = new WriterSection();

            file.Name = "File";
            file.Type = "ClownFish.Log.Serializer.FileWriter, ClownFish.Log";

            FileWriter writer = new FileWriter();

            writer.Init(file);
        }
示例#6
0
        public void Test5()
        {
            WriterSection msmq = new WriterSection();

            msmq.Name = "Msmq";
            msmq.Type = "ClownFish.Log.Serializer.MsmqWriter, ClownFish.Log";

            MsmqWriter writer = new MsmqWriter();

            writer.Init(msmq);
        }
示例#7
0
        public void Test6()
        {
            WriterSection winlog = new WriterSection();

            winlog.Name = "WinLog";
            winlog.Type = "ClownFish.Log.Serializer.WinLogWriter, ClownFish.Log";

            WinLogWriter writer = new WinLogWriter();

            writer.Init(winlog);
        }
示例#8
0
        /// <summary>
        /// 从配置文件中初始化
        /// 注意:仅供框架调用,不需要在代码中调用。
        /// </summary>
        /// <param name="config"></param>
        public void Init(WriterSection config)
        {
            string value = config.GetOptionValue("ConnectionString");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为MongoDbWriter指定ConnectionString属性。");
            }

            s_configSetting = MongoDbSetting.Create(value);
        }
示例#9
0
        public void Test4()
        {
            WriterSection mongodb = new WriterSection();

            mongodb.Name = "MongoDb";
            mongodb.Type = "ClownFish.Log.Serializer.MongoDbWriter, ClownFish.Log";

            MongoDbWriter writer = new MongoDbWriter();

            writer.Init(mongodb);
        }
示例#10
0
        public virtual void Init(WriterSection config)
        {
            // 避免重复调用
            if (s_client != null)
            {
                return;
            }

            HttpWriterClient client = ObjectFactory.New <HttpWriterClient>();

            client.Init(config);

            s_client = client;
        }
示例#11
0
        public void Test7()
        {
            WriterSection winlog = new WriterSection();

            winlog.Name = "WinLog";
            winlog.Type = "ClownFish.Log.Serializer.WinLogWriter, ClownFish.Log";

            winlog.Options    = new WriterOption[1];
            winlog.Options[0] = new WriterOption {
                Key = "LogName", Value = "ClownFish-Log"
            };

            WinLogWriter writer = new WinLogWriter();

            writer.Init(winlog);
        }
示例#12
0
        public void Init(WriterSection config)
        {
            string value = config.GetOptionValue("Receivers");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为MailWriter指定Receivers属性。");
            }


            if (s_recipients != null)
            {
                return;
            }

            s_recipients = value.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
        }
示例#13
0
        public void Init(WriterSection config)
        {
            string value = config.GetOptionValue("RootDirectory");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为FileWriter指定RootDirectory属性。");
            }


            if (s_rootDirectory != null)
            {
                return;
            }


            // 支持绝对路径,和相对路径
            string rootDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, value);


            // 检查日志根目录是否存在
            if (Directory.Exists(rootDirectory) == false)
            {
                Directory.CreateDirectory(rootDirectory);
            }


            if (rootDirectory.EndsWith("\\") == false)
            {
                rootDirectory = rootDirectory + "\\";
            }


            // 检查需要记录的各个数据类型的子目录是否存在。
            foreach (var item in WriterFactory.Config.Types)
            {
                string path = rootDirectory + item.Type.Name;
                if (Directory.Exists(path) == false)
                {
                    Directory.CreateDirectory(path);
                }
            }

            s_rootDirectory = rootDirectory;
        }
示例#14
0
        public void Init(WriterSection config)
        {
            string value = config.GetOptionValue("RootDirectory");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为JsonWriter指定RootDirectory属性。");
            }


            if (s_rootDirectory != null)
            {
                return;
            }


            s_rootDirectory = DirectoryHelper.InitDirectory(value);
        }
示例#15
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="config"></param>
        public void Init(WriterSection config)
        {
            // 避免重复调用
            if (s_url != null)
            {
                return;
            }


            string url = config.GetOptionValue("url");

            if (string.IsNullOrEmpty(url))
            {
                throw new LogConfigException("日志配置文件中,没有为HttpWriter指定url属性。");
            }

            string format = config.GetOptionValue("format");

            if (string.IsNullOrEmpty(format))
            {
                s_format = SerializeFormat.Xml;     // 默认值
            }
            else
            {
                if (Enum.TryParse <SerializeFormat>(format, out s_format) == false ||
                    s_format == SerializeFormat.None ||
                    s_format == SerializeFormat.Auto
                    )
                {
                    throw new LogConfigException("日志配置文件中,为HttpWriter指定的format属性无效,建议选择:Json or Xml");
                }
            }

            s_retryCount           = config.GetOptionValue("retry-count").TryToUInt(10);
            s_retryWaitMillisecond = config.GetOptionValue("retry-wait-millisecond").TryToUInt(1000);
            s_datatypeInHeader     = config.GetOptionValue("datatype-in-header").TryToBool(true);

            List <NameValue> queryString = ReadHttpArgs(config, "querystring:");

            s_header = ReadHttpArgs(config, "header:");

            s_url    = url.ConcatQueryStringArgs(queryString);
            s_client = new HttpWriterClient();
        }
示例#16
0
        public virtual void Init(WriterSection config)
        {
            string logName = config.GetOptionValue("LogName");

            if (string.IsNullOrEmpty(logName))
            {
                throw new LogConfigException("在配置文件中没有为WinLogWriter指定LogName属性。");
            }

            string sourceName = config.GetOptionValue("SourceName");

            if (string.IsNullOrEmpty(sourceName))
            {
                throw new LogConfigException("在配置文件中没有为WinLogWriter指定SourceName属性。");
            }

            try {
                // 尽量尝试为日志消息创建一个目录来存放

                // 下面这二个API都需要管理员权限才能调用,在ASP.NET程序中会出现异常
                // 当事件源存在时,调用SourceExists()不会出现异常,不存在时才会有异常

                if (EventLog.SourceExists(sourceName) == false)
                {
                    EventLog.CreateEventSource(sourceName, logName);
                }

                s_logName    = logName;
                s_sourceName = sourceName;
                s_initOK     = true;
            }
            catch (Exception ex) {
                string xx = ex.Message;     // 无用代码,仅用于调试查看错误原因
                // 如果权限不够,就直接存在到Application目录中。
                // 所以,这里不做异常处理
            }

            // ########### DEBUG INFO
            //EventLog.WriteEntry("Application Error ", "WinLogWriter.s_initOK: " + s_initOK.ToString());
            // ########### DEBUG INFO
        }
示例#17
0
        public virtual void Init(WriterSection config)
        {
            string value = config.GetOptionValue("RootDirectory");

            if (string.IsNullOrEmpty(value))
            {
                throw new LogConfigException("日志配置文件中,没有为FileWriter指定RootDirectory属性。");
            }

            if (s_rootDirectory != null)
            {
                return;
            }

            s_rootDirectory = DirectoryHelper.InitDirectory(value);


            string value2 = config.GetOptionValue("MaxLength");

            s_maxLength = value2.TryToUInt(100) * 1024L * 1024;
        }
示例#18
0
        public virtual void Init(WriterSection config)
        {
            string url = config.GetOptionValue("url");

            if (string.IsNullOrEmpty(url))
            {
                throw new LogConfigException("日志配置文件中,没有为HttpWriter指定url参数。");
            }

            string format = config.GetOptionValue("format");

            _format = (string.IsNullOrEmpty(format) || format.Is("json")) ? SerializeFormat.Json : SerializeFormat.Xml;


            _retryCount           = config.GetOptionValue("retry-count").TryToUInt(10);
            _retryWaitMillisecond = config.GetOptionValue("retry-wait-millisecond").TryToUInt(1000);

            _header = ReadHttpArgs(config, "header:");

            List <NameValue> queryString = ReadHttpArgs(config, "querystring:");

            _url = url.ConcatQueryStringArgs(queryString);
        }
示例#19
0
        //[TestMethod]
        public void CreaetFile()
        {
            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ClownFish.Log.config");


            LogConfig config = new LogConfig();

            config.Enable      = true;
            config.Writers     = new WriterSection[5];
            config.TimerPeriod = 100;

            WriterSection file = new WriterSection();

            file.Name       = "File";
            file.Type       = "ClownFish.Log.Serializer.FileWriter, ClownFish.Log";
            file.Options    = new WriterOption[1];
            file.Options[0] = new WriterOption {
                Key = "RootDirectory", Value = "Log"
            };
            config.Writers[0] = file;

            WriterSection mail = new WriterSection();

            mail.Name       = "Mail";
            mail.Type       = "ClownFish.Log.Serializer.MailWriter, ClownFish.Log";
            mail.Options    = new WriterOption[1];
            mail.Options[0] = new WriterOption {
                Key = "Receivers", Value = "*****@*****.**"
            };
            config.Writers[1] = mail;

#if _MongoDB_
            WriterSection mongodb = new WriterSection();
            mongodb.Name       = "MongoDb";
            mongodb.Type       = "ClownFish.Log.Serializer.MongoDbWriter, ClownFish.Log";
            mongodb.Options    = new WriterOption[1];
            mongodb.Options[0] = new WriterOption {
                Key = "ConnectionString", Value = "mongodb://10.5.106.100/Test?connectTimeout=5s;socketTimeout=5s"
            };
            config.Writers[2] = mongodb;
#endif

            WriterSection msmq = new WriterSection();
            msmq.Name       = "Msmq";
            msmq.Type       = "ClownFish.Log.Serializer.MsmqWriter, ClownFish.Log";
            msmq.Options    = new WriterOption[1];
            msmq.Options[0] = new WriterOption {
                Key = "RootPath", Value = @".\private$\ClownFish-Log-test"
            };
            config.Writers[3] = msmq;


            WriterSection winlog = new WriterSection();
            winlog.Name       = "WinLog";
            winlog.Type       = "ClownFish.Log.Serializer.WinLogWriter, ClownFish.Log";
            winlog.Options    = new WriterOption[2];
            winlog.Options[0] = new WriterOption {
                Key = "LogName", Value = "ClownFish-Log"
            };
            winlog.Options[1] = new WriterOption {
                Key = "SourceName", Value = "ClownFish-Log-Message"
            };
            config.Writers[4] = winlog;


            config.Types = new TypeItemConfig[2];
            TypeItemConfig t1 = new TypeItemConfig();
            t1.DataType     = "ClownFish.Log.Model.ExceptionInfo, ClownFish.Log";
            t1.Writers      = "MongoDb,File";
            config.Types[0] = t1;

            TypeItemConfig t2 = new TypeItemConfig();
            t2.DataType     = "ClownFish.Log.Model.PerformanceInfo, ClownFish.Log";
            t2.Writers      = "MongoDb";
            config.Types[1] = t2;

            config.Performance = new PerformanceConfig();
            config.Performance.DbExecuteTimeout   = 3;
            config.Performance.HttpExecuteTimeout = 3;

            config.ExceptionWriter = "WinLog";

            XmlHelper.XmlSerializeToFile(config, filePath, Encoding.UTF8);

            Console.WriteLine("Create Config file OK.");
        }
示例#20
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="config"></param>
 public void Init(WriterSection config)
 {
 }