@@ -18,6 +18,7 @@ class Logger
1818 public const string defaultLogName = "sessionlog" ;
1919 public static long id ;
2020
21+ private static Dictionary < string , string > sessionLogs = new Dictionary < string , string > ( ) ;
2122 private static string _lastUi = "" ;
2223 public static string LastUiLine { get { return _lastUi ; } }
2324 private static string _lastLog = "" ;
@@ -110,10 +111,9 @@ public static void LogToFile(string logStr, bool noLineBreak, string filename)
110111
111112 try
112113 {
113- if ( ! noLineBreak )
114- File . AppendAllText ( file , $ "{ Environment . NewLine } [{ id } ] [{ time } ]: { logStr } ") ;
115- else
116- File . AppendAllText ( file , " " + logStr ) ;
114+ string appendStr = noLineBreak ? $ " { logStr } " : $ "{ Environment . NewLine } [{ id } ] [{ time } ]: { logStr } ";
115+ sessionLogs [ filename ] = ( sessionLogs . ContainsKey ( filename ) ? sessionLogs [ filename ] : "" ) + appendStr ;
116+ File . AppendAllText ( file , appendStr ) ;
117117 id ++ ;
118118 }
119119 catch
@@ -122,35 +122,28 @@ public static void LogToFile(string logStr, bool noLineBreak, string filename)
122122 }
123123 }
124124
125- public static void LogIfLastLineDoesNotContainMsg ( string s , bool hidden = false , bool replaceLastLine = false , string filename = "" )
125+ public static string GetSessionLog ( string filename )
126126 {
127- if ( ! GetLastLine ( ) . Contains ( s ) )
128- Log ( s , hidden , replaceLastLine , filename ) ;
129- }
130-
131- public static void WriteToFile ( string content , bool append , string filename )
132- {
133- if ( string . IsNullOrWhiteSpace ( filename ) )
134- filename = defaultLogName ;
135-
136- if ( Path . GetExtension ( filename ) != ".txt" )
127+ if ( ! filename . Contains ( ".txt" ) )
137128 filename = Path . ChangeExtension ( filename , "txt" ) ;
138129
139- file = Path . Combine ( Paths . GetLogPath ( ) , filename ) ;
140-
141- string time = DT . Now . Month + "-" + DT . Now . Day + "-" + DT . Now . Year + " " + DT . Now . Hour + ":" + DT . Now . Minute + ":" + DT . Now . Second ;
130+ if ( sessionLogs . ContainsKey ( filename ) )
131+ return sessionLogs [ filename ] ;
132+ else
133+ return "" ;
134+ }
142135
143- try
144- {
145- if ( append )
146- File . AppendAllText ( file , Environment . NewLine + time + ":" + Environment . NewLine + content ) ;
147- else
148- File . WriteAllText ( file , Environment . NewLine + time + ":" + Environment . NewLine + content ) ;
149- }
150- catch
151- {
136+ public static List < string > GetSessionLogLastLines ( string filename , int linesCount = 5 )
137+ {
138+ string log = GetSessionLog ( filename ) ;
139+ string [ ] lines = log . SplitIntoLines ( ) ;
140+ return lines . Reverse ( ) . Take ( linesCount ) . Reverse ( ) . ToList ( ) ;
141+ }
152142
153- }
143+ public static void LogIfLastLineDoesNotContainMsg ( string s , bool hidden = false , bool replaceLastLine = false , string filename = "" )
144+ {
145+ if ( ! GetLastLine ( ) . Contains ( s ) )
146+ Log ( s , hidden , replaceLastLine , filename ) ;
154147 }
155148
156149 public static void ClearLogBox ( )
0 commit comments