예제 #1
0
 public void SaveLog(string message, EnGroup group = EnGroup.None, DateTime?date = null, short trycount = 10, [System.Runtime.CompilerServices.CallerMemberName]
                     string memberName             = "",
                     [System.Runtime.CompilerServices.CallerFilePath]
                     string sourceFilePath = "",
                     [System.Runtime.CompilerServices.CallerLineNumber]
                     int sourceLineNumber = 0)
 => Task.Run(() => SaveLog_(message, group, date, trycount, memberName, sourceFilePath, sourceLineNumber));
예제 #2
0
        private async Task SaveLog_(string message, EnGroup group, DateTime?date, short trycount,
                                    string memberName,
                                    string sourceFilePath,
                                    int sourceLineNumber)
        {
            if (!initialized)
            {
                return;
            }
            await _WriteFileSemaphore.WaitAsync();

            try
            {
                date = date ?? DateTime.Now;
                if (!string.IsNullOrEmpty(sourceFilePath))
                {
                    var info    = new FileInfo(sourceFilePath);
                    var splited = sourceFilePath.SplitString();
                    sourceFilePath = splited[splited.Count - 2];
                }
                var    msg      = $"\r\n{date}:\t{sourceFilePath}.{memberName} Line:{sourceLineNumber}\r\n {message}\r\n______________________________";
                string filePath = StartUpPath;
                if (group != EnGroup.None)
                {
                    filePath = Path.Combine(filePath, group.GetDisplay());
                }
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }

                filePath = Path.Combine(filePath, FileName);
                if (File.Exists(filePath))
                {
                    File.AppendAllLines(filePath, new List <string>()
                    {
                        msg
                    });
                }
                else
                {
                    File.WriteAllText(filePath, msg);
                }
            }
            catch (Exception ex)
            {
                if (trycount > 0)
                {
                    await Task.Delay(100);
                    await SaveLog_(message, group, date, --trycount, memberName, sourceFilePath, sourceLineNumber);

                    return;
                }
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
            }
            finally { _WriteFileSemaphore.Release(); }
        }