11using System ;
2+ using System . Collections . Generic ;
23using System . IO ;
4+ using System . Linq ;
35using System . Reflection ;
4- using System . Xml ;
56using log4net ;
67using log4net . Config ;
7- using log4net . Core ;
88
9- if ( true )
9+ const int NO_ERROR = 0 ;
10+ const int MISSING_LOGS = 1 ;
11+ const int OVERWRITTEN_LOGS = 2 ;
12+
13+ var appPath = new Uri ( Assembly . GetExecutingAssembly ( ) . Location ) . LocalPath ;
14+ var appFolder = Path . GetDirectoryName ( appPath ) ;
15+ if ( appFolder is null )
16+ {
17+ throw new InvalidOperationException (
18+ $ "Can't determine app folder for { appPath } "
19+ ) ;
20+ }
21+
22+ var logFolder = Path . Combine ( appFolder , "Logs" ) ;
23+ if ( Directory . Exists ( logFolder ) )
24+ {
25+ Directory . Delete ( logFolder , recursive : true ) ;
26+ }
27+
28+ var configFile = Path . Combine ( appFolder , "log4net.config" ) ;
29+ if ( ! File . Exists ( configFile ) )
1030{
11- var appPath = new Uri ( Assembly . GetExecutingAssembly ( ) . Location ) . LocalPath ;
12- var appFolder = Path . GetDirectoryName ( appPath ) ;
13- if ( appFolder is null )
31+ throw new InvalidOperationException ( $ "log4net.config not found at { configFile } ") ;
32+ }
33+
34+ var logCount = 10 ;
35+ var identifiers = new List < Guid > ( ) ;
36+ for ( var i = 0 ; i < 10 ; i ++ )
37+ {
38+ var identifier = Guid . NewGuid ( ) ;
39+ identifiers . Add ( identifier ) ;
40+ var logged = LogWith ( identifier , logCount ) ;
41+ if ( logged != logCount )
1442 {
15- throw new InvalidOperationException (
16- $ "Can't determine app folder for { appPath } "
17- ) ;
43+ Die ( $ "Missing logs immediately for '{ identifier } ' - found { logged } /{ logCount } ", MISSING_LOGS ) ;
1844 }
45+ }
1946
20- var configFile = Path . Combine ( appFolder , "log4net.config" ) ;
21- if ( ! File . Exists ( configFile ) )
47+ foreach ( var identifier in identifiers )
48+ {
49+ var logged = CountIdentifierInLogs ( identifier ) ;
50+ if ( logged != logCount )
2251 {
23- throw new InvalidOperationException ( $ "log4net.config not found at { configFile } " ) ;
52+ Die ( $ "Logs have been overwritten for ' { identifier } ' - found { logged } / { logCount } " , OVERWRITTEN_LOGS ) ;
2453 }
54+ }
2555
26- var info = new FileInfo ( configFile ) ;
56+ Console . WriteLine ( "All good: LOG4NET-672 is resolved" ) ;
57+ return NO_ERROR ;
2758
28- XmlConfigurator . Configure ( info ) ;
59+ void Die ( string message , int exitCode )
60+ {
61+ Console . Error . WriteLine ( message ) ;
62+ Environment . Exit ( exitCode ) ;
63+ }
64+
65+ int CountIdentifierInLogs ( Guid id )
66+ {
67+ return Directory . EnumerateFiles ( "Logs" ) . Select (
68+ filePath => CountIdentifierInFile ( id , filePath )
69+ ) . Sum ( ) ;
70+ }
71+
72+ int CountIdentifierInFile ( Guid id , string filePath )
73+ {
74+ var contents = File . ReadAllLines ( filePath ) ;
75+ return contents . Count ( line => line . Contains ( id . ToString ( ) ) ) ;
76+ }
2977
78+ int LogWith ( Guid identifier , int howManyLogs )
79+ {
80+ var info = new FileInfo ( configFile ) ;
81+ XmlConfigurator . Configure ( info ) ;
3082 var logger = LogManager . GetLogger ( "main" ) ;
3183
32- for ( var i = 0 ; i < 10 ; i ++ )
84+ for ( var i = 0 ; i < howManyLogs ; i ++ )
3385 {
34- logger . Info ( $ "test log { i } ") ;
86+ logger . Info ( $ "test log { i } [ { identifier } ] ") ;
3587 }
3688
3789 LogManager . Flush ( int . MaxValue ) ;
38- }
39-
40- // Sample.Main();
41- //
42- // public class Sample
43- // {
44- // private const string filename = "sampledata.xml";
45- //
46- // public static void Main()
47- // {
48- //
49- // XmlTextWriter writer = new XmlTextWriter (filename, null);
50- // //Use indenting for readability.
51- // writer.Formatting = Formatting.Indented;
52- //
53- // writer.WriteComment("sample XML fragment");
54- //
55- // //Write an element (this one is the root).
56- // writer.WriteStartElement("bookstore");
57- //
58- // //Write the namespace declaration.
59- // writer.WriteAttributeString("xmlns", "bk", null, "log4net");
60- //
61- // writer.WriteStartElement("book");
62- //
63- // //Lookup the prefix and then write the ISBN attribute.
64- // string prefix = writer.LookupPrefix("urn:samples");
65- // writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
66- // writer.WriteString("1-861003-78");
67- // writer.WriteEndAttribute();
68- //
69- // //Write the title.
70- // writer.WriteStartElement("title");
71- // writer.WriteString("The Handmaid's Tale");
72- // writer.WriteEndElement();
73- //
74- // //Write the price.
75- // writer.WriteElementString("price", "19.95");
76- //
77- // //Write the style element.
78- // writer.WriteStartElement(prefix, "style", "urn:samples");
79- // writer.WriteString("hardcover");
80- // writer.WriteEndElement();
81- //
82- // //Write the end tag for the book element.
83- // writer.WriteEndElement();
84- //
85- // //Write the close tag for the root element.
86- // writer.WriteEndElement();
87- //
88- // //Write the XML to file and close the writer.
89- // writer.Flush();
90- // writer.Close();
91- //
92- // //Read the file back in and parse to ensure well formed XML.
93- // XmlDocument doc = new XmlDocument();
94- // //Preserve white space for readability.
95- // doc.PreserveWhitespace = true;
96- // //Load the file
97- // doc.Load(filename);
98- //
99- // //Write the XML content to the console.
100- // Console.Write(doc.InnerXml);
101- // }
102- // }
90+ return CountIdentifierInLogs ( identifier ) ;
91+ }
0 commit comments