Skip to content

Commit

Permalink
Remove obsolete properties, methods, and classes for 3.0 (#138)
Browse files Browse the repository at this point in the history
* Remove obsolete properties, methods, and classes for 3.0. Add BinaryFormattter serialize-deserialze testing across versions, and correct serialization property bag names broken by modernization (#124).

* Seal AliasRepositoryAttribute
  • Loading branch information
erikmav committed Apr 4, 2024
1 parent 789190a commit 202bb5c
Show file tree
Hide file tree
Showing 29 changed files with 315 additions and 1,278 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using log4net.Core;
using log4net.Util;

// Serialize a LoggingEvent in 2.x format using a JSON formatter.
// This written file is read back and verified in src\log4net.Tests\Core\LoggingEventTest.cs.

// Note to avoid the need to run this program during testing,
// the output of a manual run is cached in this directory.
// If for some reason a new serialized file is needed, run this program and
// commit the result over the cached version.

var localTimestamp = new DateTime(2000, 7, 1).ToLocalTime();

var stackTrace = new StackTrace(true);

var log4net2Event = new LoggingEvent(new LoggingEventData
{
// Deliberate use of obsolete local timestamp.
#pragma warning disable CS0618 // Type or member is obsolete
TimeStamp = localTimestamp,
#pragma warning restore CS0618 // Type or member is obsolete

LoggerName = "aLogger",
Level = Level.Log4Net_Debug,
Message = "aMessage",
ThreadName = "aThread",
LocationInfo = new LocationInfo(typeof(Program)),
UserName = "aUser",
Identity = "anIdentity",
ExceptionString = "anException",
Domain = "aDomain",
Properties = new PropertiesDictionary { ["foo"] = "bar" },
});

log4net2Event.Fix = FixFlags.All;

using var stream = File.OpenWrite("SerializeV2Event.dat");
var formatter = new BinaryFormatter();
formatter.Serialize(stream, log4net2Event);
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>

<!-- Windows-only net472 because BinaryFormatter is deprecated and turned off in net8.0 -->
<TargetFramework>net472</TargetFramework>
<RootNamespace>log4net2_SerializeEvent</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="log4net" Version="2.0.17" />
</ItemGroup>

</Project>
113 changes: 113 additions & 0 deletions src/log4net.Tests/Core/LoggingEventTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

// net8 deprecates BinaryFormatter used in testing.
#if NET462_OR_GREATER

using log4net.Core;
using log4net.Util;
using NUnit.Framework;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace log4net.Tests.Core
{
[TestFixture]
public class LoggingEventTest
{
[Test]
public void SerializeDeserialize_BinaryFormatter()
{
var timestamp = new DateTime(2000, 7, 1).ToUniversalTime();
var ev = new LoggingEvent(new LoggingEventData
{
LoggerName = "aLogger",
Level = Level.Log4Net_Debug,
Message = "aMessage",
ThreadName = "aThread",
TimeStampUtc = timestamp,
LocationInfo = new LocationInfo(GetType()),
UserName = "aUser",
Identity = "anIdentity",
ExceptionString = "anException",
Domain = "aDomain",
Properties = new PropertiesDictionary { ["foo"] = "bar" },
});

var formatter = new BinaryFormatter();
using var stream = new MemoryStream();
formatter.Serialize(stream, ev);
stream.Position = 0;
var ev2 = (LoggingEvent)formatter.Deserialize(stream);

Assert.AreEqual("aLogger", ev2.LoggerName);
Assert.AreEqual(Level.Log4Net_Debug, ev2.Level);
Assert.IsNull(ev2.MessageObject);
Assert.AreEqual("aMessage", ev2.RenderedMessage);
Assert.AreEqual("aThread", ev2.ThreadName);
Assert.AreEqual(timestamp, ev2.TimeStampUtc);
Assert.IsNotNull(ev2.LocationInfo);
Assert.AreEqual("System.RuntimeMethodHandle", ev2.LocationInfo!.ClassName);
Assert.AreEqual("InvokeMethod", ev2.LocationInfo!.MethodName);
Assert.IsNull(ev2.LocationInfo!.FileName);
Assert.AreEqual("0", ev2.LocationInfo!.LineNumber);
Assert.AreEqual("aUser", ev2.UserName);
Assert.AreEqual("anIdentity", ev2.Identity);
Assert.IsNull(ev2.ExceptionObject);
Assert.AreEqual("anException", ev2.GetExceptionString());
Assert.AreEqual("aDomain", ev2.Domain);
Assert.AreEqual(1, ev.Properties.Count);
Assert.AreEqual("bar", ev2.Properties["foo"]);
}

/// <summary>
/// Loads and validates the cached serialized v2 event data from the log4net2-SerializeEvent directory.
/// </summary>
[Test]
public void DeserializeV2()
{
const string datPath = @"..\..\..\..\integration-testing\log4net2-SerializeEvent\SerializeV2Event.dat";
using var stream = File.OpenRead(datPath);
var formatter = new BinaryFormatter();
LoggingEvent ev = (LoggingEvent)formatter.Deserialize(stream);
Assert.IsNotNull(ev);

Assert.AreEqual("aLogger", ev!.LoggerName);
Assert.AreEqual(Level.Log4Net_Debug, ev.Level);
Assert.IsNull(ev.MessageObject);
Assert.AreEqual("aMessage", ev.RenderedMessage);
Assert.AreEqual("aThread", ev.ThreadName);
Assert.IsNotNull(ev.LocationInfo);
Assert.AreEqual("?", ev.LocationInfo!.ClassName);
Assert.AreEqual("?", ev.LocationInfo!.MethodName);
Assert.AreEqual("?", ev.LocationInfo!.FileName);
Assert.AreEqual("?", ev.LocationInfo!.LineNumber);
Assert.AreEqual("aUser", ev.UserName);
Assert.AreEqual("anIdentity", ev.Identity);
Assert.IsNull(ev.ExceptionObject);
Assert.AreEqual("anException", ev.GetExceptionString());
Assert.AreEqual("aDomain", ev.Domain);
Assert.AreEqual(1, ev.Properties.Count);
Assert.AreEqual("bar", ev.Properties["foo"]);
Assert.AreEqual(new DateTime(2000, 7, 1), ev.TimeStampUtc);
}
}
}
#endif // NET462_OR_GREATER
7 changes: 7 additions & 0 deletions src/log4net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\.editorconfig = ..\.editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "log4net2-SerializeEvent", "integration-testing\log4net2-SerializeEvent\log4net2-SerializeEvent.csproj", "{605058A8-CE6D-4190-91DA-7B3E9F84489E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -74,6 +76,10 @@ Global
{A4F9E417-2250-4075-9118-B4FF1C58B6C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4F9E417-2250-4075-9118-B4FF1C58B6C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4F9E417-2250-4075-9118-B4FF1C58B6C5}.Release|Any CPU.Build.0 = Release|Any CPU
{605058A8-CE6D-4190-91DA-7B3E9F84489E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{605058A8-CE6D-4190-91DA-7B3E9F84489E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{605058A8-CE6D-4190-91DA-7B3E9F84489E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{605058A8-CE6D-4190-91DA-7B3E9F84489E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -85,6 +91,7 @@ Global
{169118A6-CAC3-4E83-ABEE-9E884B75B294} = {8953473C-EEE8-4740-993D-B8E10FA876CD}
{A4F9E417-2250-4075-9118-B4FF1C58B6C5} = {8953473C-EEE8-4740-993D-B8E10FA876CD}
{92B16708-35B8-4E60-AF44-CF6D797FB63B} = {3EE6A25B-2DF9-4825-A784-EE32EEA0BE58}
{605058A8-CE6D-4190-91DA-7B3E9F84489E} = {8953473C-EEE8-4740-993D-B8E10FA876CD}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {532DE291-8626-4FE3-B255-78515D744BCF}
Expand Down
26 changes: 1 addition & 25 deletions src/log4net/Appender/BufferingAppenderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,31 +184,7 @@ protected BufferingAppenderSkeleton(bool eventMustBeFixed)
public ITriggeringEventEvaluator? LossyEvaluator { get; set; }

/// <summary>
/// Gets or sets a value indicating if only part of the logging event data
/// should be fixed.
/// </summary>
/// <value>
/// <c>true</c> if the appender should only fix part of the logging event
/// data, otherwise <c>false</c>. The default is <c>false</c>.
/// </value>
/// <remarks>
/// <para>
/// Setting this property to <c>true</c> will cause only part of the
/// event data to be fixed and serialized. This will improve performance.
/// </para>
/// <para>
/// See <see cref="M:LoggingEvent.FixVolatileData(FixFlags)"/> for more information.
/// </para>
/// </remarks>
[Obsolete("Use Fix property")]
public virtual bool OnlyFixPartialEventData
{
get => Fix == FixFlags.Partial;
set => Fix = value ? FixFlags.Partial : FixFlags.All;
}

/// <summary>
/// Gets or sets the fields that will be fixed in the event
/// Gets or sets the fields that will be fixed in the event.
/// </summary>
/// <value>
/// The event fields that will be fixed before the event is buffered
Expand Down
32 changes: 0 additions & 32 deletions src/log4net/Appender/ColoredConsoleAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,38 +148,6 @@ public ColoredConsoleAppender()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ColoredConsoleAppender" /> class
/// with the specified layout.
/// </summary>
/// <param name="layout">the layout to use for this appender</param>
/// <remarks>
/// The instance of the <see cref="ColoredConsoleAppender" /> class is set up to write
/// to the standard output stream.
/// </remarks>
[Obsolete("Instead use the default constructor and set the Layout property")]
public ColoredConsoleAppender(ILayout layout) : this(layout, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ColoredConsoleAppender" /> class
/// with the specified layout.
/// </summary>
/// <param name="layout">the layout to use for this appender</param>
/// <param name="writeToErrorStream">flag set to <c>true</c> to write to the console error stream</param>
/// <remarks>
/// When <paramref name="writeToErrorStream" /> is set to <c>true</c>, output is written to
/// the standard error output stream. Otherwise, output is written to the standard
/// output stream.
/// </remarks>
[Obsolete("Instead use the default constructor and set the Layout & Target properties")]
public ColoredConsoleAppender(ILayout layout, bool writeToErrorStream)
{
Layout = layout;
m_writeToErrorStream = writeToErrorStream;
}

/// <summary>
/// Target is the value of the console output stream.
/// This is either <c>"Console.Out"</c> or <c>"Console.Error"</c>.
Expand Down
32 changes: 0 additions & 32 deletions src/log4net/Appender/ConsoleAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,38 +62,6 @@ public ConsoleAppender()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleAppender" /> class
/// with the specified layout.
/// </summary>
/// <param name="layout">the layout to use for this appender</param>
/// <remarks>
/// The instance of the <see cref="ConsoleAppender" /> class is set up to write
/// to the standard output stream.
/// </remarks>
[Obsolete("Instead use the default constructor and set the Layout property")]
public ConsoleAppender(ILayout layout) : this(layout, false)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleAppender" /> class
/// with the specified layout.
/// </summary>
/// <param name="layout">the layout to use for this appender</param>
/// <param name="writeToErrorStream">flag set to <c>true</c> to write to the console error stream</param>
/// <remarks>
/// When <paramref name="writeToErrorStream" /> is set to <c>true</c>, output is written to
/// the standard error output stream. Otherwise, output is written to the standard
/// output stream.
/// </remarks>
[Obsolete("Instead use the default constructor and set the Layout & Target properties")]
public ConsoleAppender(ILayout layout, bool writeToErrorStream)
{
Layout = layout;
m_writeToErrorStream = writeToErrorStream;
}

/// <summary>
/// Target is the value of the console output stream.
/// This is either <c>"Console.Out"</c> or <c>"Console.Error"</c>.
Expand Down
28 changes: 0 additions & 28 deletions src/log4net/Appender/DebugAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,6 @@ namespace log4net.Appender
/// <author>Nicko Cadell</author>
public class DebugAppender : AppenderSkeleton
{
/// <summary>
/// Initializes a new instance of the <see cref="DebugAppender" />.
/// </summary>
/// <remarks>
/// <para>
/// Default constructor.
/// </para>
/// </remarks>
public DebugAppender()
{
}

/// <summary>
/// Initializes a new instance of the <see cref="DebugAppender" />
/// with a specified layout.
/// </summary>
/// <param name="layout">The layout to use with this appender.</param>
/// <remarks>
/// <para>
/// Obsolete constructor.
/// </para>
/// </remarks>
[System.Obsolete("Instead use the default constructor and set the Layout property")]
public DebugAppender(ILayout layout)
{
Layout = layout;
}

/// <summary>
/// Gets or sets a value that indicates whether the appender will
/// flush at the end of each write.
Expand Down
16 changes: 0 additions & 16 deletions src/log4net/Appender/EventLogAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,6 @@ public EventLogAppender()
MachineName = "."; // Only log on the local machine
}

/// <summary>
/// Initializes a new instance of the <see cref="EventLogAppender" /> class
/// with the specified <see cref="ILayout" />.
/// </summary>
/// <param name="layout">The <see cref="ILayout" /> to use with this appender.</param>
/// <remarks>
/// <para>
/// Obsolete constructor.
/// </para>
/// </remarks>
[Obsolete("Instead use the default constructor and set the Layout property")]
public EventLogAppender(ILayout layout) : this()
{
Layout = layout;
}

/// <summary>
/// The name of the log where messages will be stored.
/// </summary>
Expand Down

0 comments on commit 202bb5c

Please sign in to comment.