Skip to content

Commit 54ca7c3

Browse files
committed
⚗️ provide "proper" namespacing on netstandard only, reverting to log4net:event and similar for .net framework
1 parent acc55ec commit 54ca7c3

File tree

6 files changed

+210
-2
lines changed

6 files changed

+210
-2
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
using System.Xml;
5+
using log4net;
6+
using log4net.Config;
7+
using log4net.Core;
8+
9+
if (true)
10+
{
11+
var appPath = new Uri(Assembly.GetExecutingAssembly().Location).LocalPath;
12+
var appFolder = Path.GetDirectoryName(appPath);
13+
if (appFolder is null)
14+
{
15+
throw new InvalidOperationException(
16+
$"Can't determine app folder for {appPath}"
17+
);
18+
}
19+
20+
var configFile = Path.Combine(appFolder, "log4net.config");
21+
if (!File.Exists(configFile))
22+
{
23+
throw new InvalidOperationException($"log4net.config not found at {configFile}");
24+
}
25+
26+
var info = new FileInfo(configFile);
27+
28+
XmlConfigurator.Configure(info);
29+
30+
var logger = LogManager.GetLogger("main");
31+
32+
for (var i = 0; i < 10; i++)
33+
{
34+
logger.Info($"test log {i}");
35+
}
36+
37+
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+
// }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>log4net_672</RootNamespace>
7+
<ImplicitUsings>disable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<LangVersion>preview</LangVersion>
10+
<DefineConstants>NETSTANDARD</DefineConstants>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\log4net\log4net.csproj" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<None Update="log4net.config">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<log4net>
2+
<appender name="LogFileAppenderXML" type="log4net.Appender.RollingFileAppender">
3+
<file value="Logs\.xml" />
4+
<datePattern value="yyyy-MM-dd-'Xyz'" />
5+
<appendToFile value="true" />
6+
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
7+
<rollingStyle value="Composite" />
8+
<maxSizeRollBackups value="40" />
9+
<maximumFileSize value="1KB" />
10+
<preserveLogFileNameExtension value="true" />
11+
<staticLogFileName value="false" />
12+
<layout type="log4net.Layout.XmlLayout"/>
13+
</appender>
14+
<root>
15+
<level value="ALL"/>
16+
<appender-ref ref="LogFileAppenderXML" />
17+
</root>
18+
</log4net>

src/log4net.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "log4net", "log4net\log4net.
2424
EndProject
2525
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "log4net.Tests", "log4net.Tests\log4net.Tests.csproj", "{B0530F10-0238-49A9-93B0-8EF412E90BCF}"
2626
EndProject
27+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "integration-testing", "integration-testing", "{8953473C-EEE8-4740-993D-B8E10FA876CD}"
28+
EndProject
29+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "log4net-672", "integration-testing\log4net-672\log4net-672.csproj", "{9050922E-E22E-4FF5-A1B0-2F21745C6882}"
30+
EndProject
2731
Global
2832
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2933
Debug|Any CPU = Debug|Any CPU
@@ -43,8 +47,17 @@ Global
4347
{B0530F10-0238-49A9-93B0-8EF412E90BCF}.Release|Any CPU.Build.0 = Release|Any CPU
4448
{B0530F10-0238-49A9-93B0-8EF412E90BCF}.CrossPlatform|Any CPU.ActiveCfg = CrossPlatform|Any CPU
4549
{B0530F10-0238-49A9-93B0-8EF412E90BCF}.CrossPlatform|Any CPU.Build.0 = CrossPlatform|Any CPU
50+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
51+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.Debug|Any CPU.Build.0 = Debug|Any CPU
52+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.Release|Any CPU.ActiveCfg = Release|Any CPU
53+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.Release|Any CPU.Build.0 = Release|Any CPU
54+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.CrossPlatform|Any CPU.ActiveCfg = Debug|Any CPU
55+
{9050922E-E22E-4FF5-A1B0-2F21745C6882}.CrossPlatform|Any CPU.Build.0 = Debug|Any CPU
4656
EndGlobalSection
4757
GlobalSection(SolutionProperties) = preSolution
4858
HideSolutionNode = FALSE
4959
EndGlobalSection
60+
GlobalSection(NestedProjects) = preSolution
61+
{9050922E-E22E-4FF5-A1B0-2F21745C6882} = {8953473C-EEE8-4740-993D-B8E10FA876CD}
62+
EndGlobalSection
5063
EndGlobal

src/log4net/Layout/XmlLayout.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ public bool Base64EncodeProperties
160160
set {m_base64Properties=value;}
161161
}
162162

163-
164163
#endregion Public Instance Properties
165164

166165
#region Implementation of IOptionHandler
@@ -199,7 +198,7 @@ public override void ActivateOptions()
199198
m_elmLocation = m_prefix + ":" + ELM_LOCATION;
200199
}
201200
}
202-
201+
203202
#endregion Implementation of IOptionHandler
204203

205204
#region Override implementation of XMLLayoutBase
@@ -217,7 +216,12 @@ public override void ActivateOptions()
217216
/// </remarks>
218217
protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
219218
{
219+
#if NETSTANDARD
220+
writer.WriteStartElement(m_prefix, ELM_EVENT, m_prefix);
221+
// writer.WriteAttributeString("xmlns", "log4net", null, "http://logging.apache.org/log4net/schemas/log4net-events-1.2");
222+
#else
220223
writer.WriteStartElement(m_elmEvent);
224+
#endif
221225
writer.WriteAttributeString(ATTR_LOGGER, loggingEvent.LoggerName);
222226

223227
#if NET_2_0 || NETCF_2_0 || MONO_2_0 || NETSTANDARD
@@ -243,7 +247,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
243247
}
244248

245249
// Append the message text
250+
#if NETSTANDARD
251+
writer.WriteStartElement(m_prefix, ELM_MESSAGE, m_prefix);
252+
#else
246253
writer.WriteStartElement(m_elmMessage);
254+
#endif
247255
if (!this.Base64EncodeMessage)
248256
{
249257
Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage, this.InvalidCharReplacement);
@@ -261,10 +269,18 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
261269
// Append the properties text
262270
if (properties.Count > 0)
263271
{
272+
#if NETSTANDARD
273+
writer.WriteStartElement(m_prefix, ELM_PROPERTIES, m_prefix);
274+
#else
264275
writer.WriteStartElement(m_elmProperties);
276+
#endif
265277
foreach(System.Collections.DictionaryEntry entry in properties)
266278
{
279+
#if NETSTANDARD
280+
writer.WriteStartElement(m_prefix, ELM_DATA, m_prefix);
281+
#else
267282
writer.WriteStartElement(m_elmData);
283+
#endif
268284
writer.WriteAttributeString(ATTR_NAME, Transform.MaskXmlInvalidCharacters((string)entry.Key,this.InvalidCharReplacement));
269285

270286
// Use an ObjectRenderer to convert the object to a string
@@ -289,7 +305,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
289305
if (exceptionStr != null && exceptionStr.Length > 0)
290306
{
291307
// Append the stack trace line
308+
#if NETSTANDARD
309+
writer.WriteStartElement(m_prefix, ELM_EXCEPTION, m_prefix);
310+
#else
292311
writer.WriteStartElement(m_elmException);
312+
#endif
293313
Transform.WriteEscapedXmlString(writer, exceptionStr,this.InvalidCharReplacement);
294314
writer.WriteEndElement();
295315
}
@@ -298,7 +318,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
298318
{
299319
LocationInfo locationInfo = loggingEvent.LocationInformation;
300320

321+
#if NETSTANDARD
322+
writer.WriteStartElement(m_prefix, ELM_LOCATION, m_prefix);
323+
#else
301324
writer.WriteStartElement(m_elmLocation);
325+
#endif
302326
writer.WriteAttributeString(ATTR_CLASS, locationInfo.ClassName);
303327
writer.WriteAttributeString(ATTR_METHOD, locationInfo.MethodName);
304328
writer.WriteAttributeString(ATTR_FILE, locationInfo.FileName);

src/log4net/Layout/XmlLayoutSchemaLog4j.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
172172
}
173173

174174
// Write the start element
175+
#if NETSTANDARD
175176
writer.WriteStartElement("log4j", "event", "log4net");
177+
#else
178+
writer.WriteStartElement("log4j:event");
179+
#endif
176180
writer.WriteAttributeString("logger", loggingEvent.LoggerName);
177181

178182
// Calculate the timestamp as the number of milliseconds since january 1970
@@ -187,7 +191,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
187191
writer.WriteAttributeString("thread", loggingEvent.ThreadName);
188192

189193
// Append the message text
194+
#if NETSTANDARD
190195
writer.WriteStartElement("log4j", "message", "log4net");
196+
#else
197+
writer.WriteStartElement("log4j:message");
198+
#endif
191199
Transform.WriteEscapedXmlString(writer, loggingEvent.RenderedMessage,this.InvalidCharReplacement);
192200
writer.WriteEndElement();
193201

@@ -199,7 +207,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
199207
if (valueStr != null && valueStr.Length > 0)
200208
{
201209
// Append the NDC text
210+
#if NETSTANDARD
202211
writer.WriteStartElement("log4j", "NDC", "log4net");
212+
#else
213+
writer.WriteStartElement("log4j:NDC");
214+
#endif
203215
Transform.WriteEscapedXmlString(writer, valueStr,this.InvalidCharReplacement);
204216
writer.WriteEndElement();
205217
}
@@ -209,10 +221,18 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
209221
PropertiesDictionary properties = loggingEvent.GetProperties();
210222
if (properties.Count > 0)
211223
{
224+
#if NETSTANDARD
212225
writer.WriteStartElement("log4j", "properties", "log4net");
226+
#else
227+
writer.WriteStartElement("log4j:properties");
228+
#endif
213229
foreach(System.Collections.DictionaryEntry entry in properties)
214230
{
231+
#if NETSTANDARD
215232
writer.WriteStartElement("log4j", "data", "log4net");
233+
#else
234+
writer.WriteStartElement("log4j:data");
235+
#endif
216236
writer.WriteAttributeString("name", (string)entry.Key);
217237

218238
// Use an ObjectRenderer to convert the object to a string
@@ -228,7 +248,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
228248
if (exceptionStr != null && exceptionStr.Length > 0)
229249
{
230250
// Append the stack trace line
251+
#if NETSTANDARD
231252
writer.WriteStartElement("log4j", "throwable", "log4net");
253+
#else
254+
writer.WriteStartElement("log4j:throwable");
255+
#endif
232256
Transform.WriteEscapedXmlString(writer, exceptionStr,this.InvalidCharReplacement);
233257
writer.WriteEndElement();
234258
}
@@ -237,7 +261,11 @@ protected override void FormatXml(XmlWriter writer, LoggingEvent loggingEvent)
237261
{
238262
LocationInfo locationInfo = loggingEvent.LocationInformation;
239263

264+
#if NETSTANDARD
240265
writer.WriteStartElement("log4j", "locationInfo", "log4net");
266+
#else
267+
writer.WriteStartElement("log4j:locationInfo");
268+
#endif
241269
writer.WriteAttributeString("class", locationInfo.ClassName);
242270
writer.WriteAttributeString("method", locationInfo.MethodName);
243271
writer.WriteAttributeString("file", locationInfo.FileName);

0 commit comments

Comments
 (0)