コード例 #1
0
        /// <summary>
        /// 특정 파일에 기록할 로그를 작성한다. (주로 주문정보에 사용해야 함.)
        /// 예) D:\[로그폴더]\S20181231000111.txt 형식으로 기록함.
        /// </summary>
        /// <param name="fullFileName">전체파일이름 (경로 + 파일명)</param>
        /// <param name="owner">소유자(메서드 또는 클래스.메서드)</param>
        /// <param name="title">로그 제목</param>
        /// <param name="message"></param>
        /// <param name="logCode"></param>
        /// <param name="e"></param>
        public static void WriteLog(string fullFileName, string owner, string title, string message, LogCode logCode = LogCode.Information, Exception e = null)
        {
            var fullFilePath = Path.GetDirectoryName(fullFileName);

            if (!Directory.Exists(fullFilePath))
            {
                Directory.CreateDirectory(fullFilePath);
            }

            var fullLog = string.Empty;

            if (e == null)
            {
                fullLog += $"{DateTime.Now.ToString("HH:mm:ss")} ({logCode.ToString()} [{owner}] [{title}] : {message})";
            }
            else
            {
                fullLog += $"{DateTime.Now.ToString("HH:mm:ss")} ({logCode.ToString()} [{owner}] [{title}] : {message})" + Environment.NewLine;
                fullLog += "Exception Message : " + e.Message + Environment.NewLine;
                fullLog += "Stack Trace : " + e.StackTrace;
            }

            var ext = Path.GetExtension(fullFileName);
            var fileNameWithoutExt = Path.GetFileNameWithoutExtension(fullFileName);
            var lastExtIndex       = fullFileName.LastIndexOf(".", StringComparison.CurrentCulture);

            fullFileName = fullFileName.Substring(0, lastExtIndex) + DateTime.Now.ToString("yyyyMMdd") + ext;

            try
            {
                using (var sw = new StreamWriter(fullFileName, true, Encoding.UTF8, 4096))
                {
                    sw.WriteLine(fullLog);
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch (Exception ex)
            {
                if (!EventLog.SourceExists(_source))
                {
                    EventLog.CreateEventSource(_source, _log);
                    EventLog.WriteEntry(_source, ex.Message, EventLogEntryType.Error, 234);
                }
                else
                {
                    EventLog.WriteEntry(_source, ex.Message, EventLogEntryType.Error, 234);
                }
            }
        }
コード例 #2
0
        public void WriteLog(string strLog, LogCode logCode)
        {
            try
            {
                _con.Open();

                var transaction = _con.BeginTransaction();

                var command = _con.CreateCommand();
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "";
                command.Parameters.AddRange(new SqlParameter[]
                {
                    new SqlParameter {
                        ParameterName = "LogText", Value = strLog
                    },
                    new SqlParameter {
                        ParameterName = "LogCode", Value = logCode.ToString()
                    }
                });
                command.ExecuteNonQuery();

                transaction.Commit();
            }
            catch
            {
                throw;
            }
            finally
            {
                _con.Close();
            }
        }
コード例 #3
0
        public static void WriteLog(string strLog, LogCode logCode, string strFileName, string strPath)
        {
            string strFullName;

            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }

            if (strPath.EndsWith(@"\") == false || strPath.EndsWith("/") == false)
            {
                strPath = strPath + @"\";
            }

            strFullName = strPath + strFileName + "_" + DateTime.Now.ToString(DateTimeFormet) + ".txt";

            string strFullLog = DateTime.Now.ToString("HH:mm:ss") + " (" + logCode.ToString() + ")" + " : " + strLog;

            lock (asyncObject)
            {
                using (StreamWriter sw = new StreamWriter(strFullName, true, System.Text.Encoding.UTF8, 4096))
                {
                    sw.WriteLine(strFullLog);
                    sw.Close();
                }
            }
        }
コード例 #4
0
ファイル: Logger.cs プロジェクト: ServerBP/BeatsaberBot
        public async Task Log(LogCode code, string message, SocketMessage messageInfo = null)
        {
            if (message.Length > 2000)
            {
                return;
            }
            var color = Color.Green;

            switch (code)
            {
            case LogCode.error:
                color = Color.Red;
                break;

            case LogCode.warning:
                color = Color.Orange;
                break;

            case LogCode.debug:
                color = Color.Blue;
                break;

            case LogCode.message:
                color = Color.Green;
                break;

            case LogCode.fatal_error:
                color   = Color.DarkRed;
                message = "<@138439306774577152> \n" + message;
                break;
            }

            var embedbuilder = new EmbedBuilder
            {
                Title       = code.ToString(),
                Description = message,
                Color       = color,
                Footer      = new EmbedFooterBuilder()
                {
                    Text = DateTime.Now.ToString()
                }
            };

            //Console.WriteLine(embedbuilder.Title + "\n\n" + embedbuilder.Description);
            try
            {
                await _discord.GetGuild(505485680344956928).GetTextChannel(592877335355850752).SendMessageAsync("", false, embedbuilder.Build());

                //await _discord.GetGuild(677437721081413633).GetTextChannel(682874265594363916).SendMessageAsync("", false, embedbuilder.Build());
            }
            catch
            {
                Console.WriteLine("No Internet connection");
            }
        }
コード例 #5
0
        public static string FormatMessage(LogCode logCode, params object[] args)
        {
            LogMessage logMessage;

            if (!LogMessageDictionary.TryGetValue(logCode.ToString(), out logMessage))
            {
                throw new ApplicationException($"Log message is not defined for log code: {logCode}.");
            }

            try
            {
                string message = string.Format(logMessage.Message, args);
                return(string.IsNullOrEmpty(message) ? logCode.ToString() : message);
            }
            catch (FormatException fe)
            {
                var argsString = args == null ? "<null>" : string.Join(", ", args);
                throw new ApplicationException($"Log message {logMessage} for log code {logCode} has less paramters than provided {argsString}", fe);
            }
        }
コード例 #6
0
ファイル: Logging.cs プロジェクト: p-skakun/glTFast
        public static string GetFullMessage(LogCode code, params string[] messages)
        {
#if GLTFAST_REPORT
            return(messages != null
                ? string.Format(fullMessages[code], messages)
                : fullMessages[code]);
#else
            if (messages == null)
            {
                return(code.ToString());
            }
            else
            {
                var sb = new StringBuilder(code.ToString());
                foreach (var message in messages)
                {
                    sb.Append(";");
                    sb.Append(message);
                }
                return(sb.ToString());
            }
#endif
        }