示例#1
0
        private static void LogImpl(object log, LogTarget target)
        {
            if (target.HasFlag(LogTarget.Console))
            {
                Console.WriteLine(log);
            }

            if (target.HasFlag(LogTarget.File))
            {
                File.AppendAllLines(FileName, new[] { log.ToString() });
            }

            if (target.HasFlag(LogTarget.Main))
            {
                Program.MainForm.WriteLine(log.ToString());
            }
        }
示例#2
0
文件: Logger.cs 项目: Sinjai/MassCopy
        // Explicit static constructor to tell C# compiler
        //  not to mark type as beforefieldinit
        // Enables laziness (http://csharpindepth.com/Articles/General/Singleton.aspx)
        //static Logger()
        //{
        //}
        #endregion

        private void Log(string message, LogType logType, LogTarget logTarget)
        {
            // Prepend datetime and log type
            message = $"[{CurrentDateTime}] {logType.ToString().ToUpper()}: {message}";

            if (logTarget.HasFlag(LogTarget.TextBox))
            {
                LogBox.AppendLine(message);
            }

            if (logTarget.HasFlag(LogTarget.File))
            {
                sw.WriteLine(message);

                // Flush the buffer every line, for now
                sw.Flush();
                fs.Flush();
            }
        }
示例#3
0
        /// <summary>
        ///     Writes one line to the log.
        /// </summary>
        public static void Write <T>(string type, ConsoleColor color, T value, LogTarget target = LogTarget.Default)
        {
            if (target == LogTarget.Default)
            {
                target = DefaultTarget;
            }

            if (target.HasFlag(LogTarget.Console))
            {
                ColorfulWriteLine(color, ProcessPreset(LogTarget.Console, type, value.ToString()));
            }
            if (target.HasFlag(LogTarget.Debugger))
            {
                Debug.WriteLine(ProcessPreset(LogTarget.Debugger, type, value.ToString()));
            }
            if (target.HasFlag(LogTarget.File))
            {
                _logStream?.WriteLine(ProcessPreset(LogTarget.File, type, value.ToString()));
            }
        }
示例#4
0
        /// <summary>
        /// Method for logging, calling this method will store the message on the specific target
        /// </summary>
        /// <param name="logType">Specify the type of the message (Message, Warining or Error)</param>
        /// <param name="target">Specify where the record will be stored</param>
        /// <param name="message">Specify the message of the log.</param>
        public static void LogMessage(LogType logType, LogTarget target, string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }
            message = message.Trim();
            if (target.HasFlag(LogTarget.Console))
            {
                switch (logType)
                {
                case LogType.Message:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;

                case LogType.Error:
                    Console.ForegroundColor = ConsoleColor.Red;
                    break;

                case LogType.Warning:
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    break;

                default:
                    Console.ForegroundColor = ConsoleColor.White;
                    break;
                }
                Console.WriteLine(string.Format("{0} {1}", DateTime.Today.ToString("yyyyMMdd"), message));
            }
            if (target.HasFlag(LogTarget.File))
            {
                try
                {
                    string path    = System.IO.Path.Combine(System.Configuration.ConfigurationManager.AppSettings["Log_FileDirectory"], string.Concat(CONST_LogFile_Prefix, DateTime.Today.ToString("yyyyMMdd"), ".txt"));
                    string content = string.Empty;
                    if (System.IO.File.Exists(path))
                    {
                        content = System.IO.File.ReadAllText(path);
                    }
                    StringBuilder sBuilder = new StringBuilder(content);
                    sBuilder.AppendLine(string.Format("{0} {1}", DateTime.Today.ToString("yyyyMMdd"), message));
                    System.IO.File.WriteAllText(path, sBuilder.ToString());
                }
                catch (System.IO.IOException ioExc)
                {
                    throw ioExc;
                }
            }
            if (target.HasFlag(LogTarget.Database))
            {
                System.Data.SqlClient.SqlConnection connection = null;
                using (connection = new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    try
                    {
                        System.Data.SqlClient.SqlCommand command = connection.CreateCommand();
                        command.CommandText = "exec InsertintoLogValues '" + message + "'," + Convert.ToString(logType) + "";
                        command.Connection  = connection;
                        connection.Open();
                        command.ExecuteNonQuery();
                    }
                    catch (System.Data.SqlClient.SqlException sqlExc)
                    {
                        throw sqlExc;
                    }
                    finally
                    {
                        if (connection.State == System.Data.ConnectionState.Open)
                        {
                            connection.Close();
                        }
                    }
                }
            }
        }