### Unity Package Installation Git URL - Text Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Provides the Git URL required to install the Unity.Extensions.Logging package via Unity's Package Manager using the 'Install from Git URL' option. The placeholder must be replaced with a valid branch name from the UnityUtil repository. ```txt https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/Unity.Extensions.Logging# ``` -------------------------------- ### Installing Serilog Unity Sink via Git URL - Text Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This snippet provides the Git URL required to install the Serilog.Sinks.Unity package directly into a Unity project using the Package Manager's "Add package from Git URL" feature. Users must replace with a valid branch name like unity6. This is the primary method for installing the package. ```text https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/Serilog.Sinks.Unity# ``` -------------------------------- ### Using UnityDebugLoggerFactory - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Provides an example of how to directly instantiate and use the default UnityDebugLoggerFactory and UnityDebugLogger types. This setup sends log messages directly to the Unity Console without requiring a full Microsoft.Extensions.Logging pipeline, useful for quick debugging in tests or editor scripts. ```csharp using Unity.Extensions.Logging; ILogger logger = new UnityDebugLoggerFactory().CreateLogger(); logger.LogInformation("Hello, world!"); ``` -------------------------------- ### Initializing Serilog Unity Sink Default - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates the basic configuration to initialize Serilog using the Unity sink with default settings. It creates a new LoggerConfiguration, adds the WriteTo.Unity() sink, and builds the logger. This setup directs log messages to Unity's default logger without custom formatting or tagging. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity() .CreateLogger(); ``` -------------------------------- ### Adding Unity RemoteConfig with AddInMemoryCollection C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil.Configuration.RemoteConfig/README.md Presents the recommended approach for integrating Unity RemoteConfig by first awaiting `FetchConfigsAsync` and then adding the fetched key-value pairs to the configuration builder via `AddInMemoryCollection`. This avoids the deadlock issues associated with the custom provider approach and is executed within an asynchronous Unity `Start` method. ```C# private async void Start() { IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); // Add lower-priority configuration sources... if (!Application.isEditor || Application.isPlaying) { RemoteConfigService.Instance.SetEnvironmentID(/* ... */); // Init other RemoteConfig properties RuntimeConfig runtimeConfig = await RemoteConfigService.Instance.FetchConfigsAsync(new TUser(), new TApp()); configurationBuilder.AddInMemoryCollection( runtimeConfig.GetKeys().Select(x => KeyValuePair.Create(x, (string?)runtimeConfig.GetString(x))) ); } // Add higher-priority configuration sources... IConfigurationRoot config = configurationBuilder.Build(); } ``` -------------------------------- ### Configuring Serilog Default Unity Enricher (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Enrichers.Unity/README.md Demonstrates the basic configuration of the Serilog logger to include the Unity data enricher with its default settings. This requires the Serilog library and the Serilog.Enrichers.Unity package installed. The snippet shows chaining configuration methods to build the logger instance. ```C# var logger = new Serilog.LoggerConfiguration() .Enrich.WithUnityData() // Configure other Serilog enrichers, sinks, etc. .CreateLogger(); ``` -------------------------------- ### Enabling Serilog SelfLog to Unity Console C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures Serilog's internal SelfLog mechanism to redirect logging pipeline exceptions and errors to the Unity Console using UnityEngine.Debug.LogError (or .LogWarning). This is essential for diagnosing configuration or runtime issues within the Serilog setup itself. Requires Serilog.SelfLog. ```C# SelfLog.Enable(UnityEngine.Debug.LogError); // Or .LogWarning() ``` -------------------------------- ### Configuring Root Serilog Logger From LogContext - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet configures a root Serilog logger that is enriched from the LogContext and writes to the Unity sink using default settings. By default, the Unity sink uses "SourceContext" as the tag property name. This setup allows child loggers created with ForContext() to automatically provide the type name as the tag via the SourceContext property. ```C# var rootLogger = new Serilog.LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Unity() .CreateLogger(); ``` -------------------------------- ### Adding Serilog Packages - Unity UPM - JSON Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil/README.md Explains how to configure the Unity Package Manager's `manifest.json` file to include Serilog and its Microsoft.Extensions.Logging adapter using the UnityNuGet scoped registry. Required dependencies are Serilog and Serilog.Extensions.Logging NuGet packages. ```json // Packages/manifest.json { "dependencies": { "org.nuget.serilog": ", "org.nuget.serilog.extensions.logging": "", // ... }, // ... } ``` -------------------------------- ### Using UnityDebugLoggerFactory - Unity Logging - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil/README.md Demonstrates how to directly instantiate UnityDebugLoggerFactory and use it to obtain an ILogger instance. This logger is configured to write output directly to the Unity Console, providing a simple logging solution without dependency injection. Useful for scenarios like editor tools or unit tests. ```csharp class MyClass { public void DoStuff() { ILogger logger = new UnityDebugLoggerFactory().CreateLogger(); logger.LogInformation(new EventId(100, nameof(DoStuff)), "Currently doing stuff in {frame}...", Time.frameCount); } } ``` -------------------------------- ### Add Package via Unity Package Manager Git URL Source: https://github.com/derploidentertainment/unityutil/blob/main/README.md Provides various Git URL formats used to add specific UnityUtil sub-packages and versions to a Unity project through the Package Manager. These URLs specify the repository, the path within the repo, and the desired branch or tag. ```URL https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/#main ``` ```URL https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/#unity ``` ```URL https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/#-unity ``` ```URL https://github.com/DerploidEntertainment/UnityUtil.git?path=/UnityUtil/Assets/#unity-dev ``` -------------------------------- ### Using Logger Extension Method - .NET Logging - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil/README.md Shows a C# class calling a custom logger extension method (DoingStuff) defined elsewhere. This demonstrates how extension methods simplify logging calls, making the main logic cleaner and more readable. Relies on the custom LoggerExtensions class being available. ```csharp // In MyClass.cs class MyClass { public void DoStuff() { ILogger logger = new UnityDebugLoggerFactory().CreateLogger(); logger.DoingStuff(Time.frameCount); } } ``` -------------------------------- ### Initializing Serilog Unity Sink Custom Logger - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet illustrates how to configure the Serilog Unity sink to use a custom implementation of UnityEngine.ILogger. It passes an instance of MyCustomLogger (as defined in the previous snippet) to the WriteTo.Unity configuration method. This allows logs to be routed through a user-provided logger instead of the default UnityEngine.Debug.unityLogger. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(logger: new MyCustomLogger()) .CreateLogger(); ``` -------------------------------- ### Initializing Serilog Unity Sink Custom Formatting - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows how to configure the Serilog Unity sink with a custom output template and a specific format provider. It uses the outputTemplate parameter to define the message format and the formatProvider parameter to specify regional formatting rules, in this case, for Spanish (Mexico). This allows for flexible control over the appearance of log messages in the Unity console. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity( outputTemplate: "{Message:lj} is my message", formatProvider: CultureInfo.GetCulture("es-MX") ) .CreateLogger(); ``` -------------------------------- ### Injecting and Using ILogger - .NET/Unity - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil/README.md Shows a C# class receiving an ILoggerFactory via an Inject method and creating a type-specific ILogger. It demonstrates logging an informational message with an EventId and structured parameters using the created logger instance. Requires configuration of an ILoggerFactory via dependency injection. ```csharp class MyClass { private ILogger? _logger; // ... public void Inject(ILoggerFactory loggerFactory) { _logger = loggerFactory.CreateLogger(); } // ... public void DoStuff() { _logger!.LogInformation(new EventId(100, nameof(DoStuff)), "Currently doing stuff in {frame}...", Time.frameCount); } } ``` -------------------------------- ### Initializing Serilog Unity Sink Custom ITextFormatter - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates how to use a custom ITextFormatter implementation with the Serilog Unity sink. It passes an instance of CompactJsonFormatter (presumably from Serilog.Formatting.Compact) to the WriteTo.Unity method. This is useful for scenarios where standard text formatting is insufficient, allowing for complex or compact formats like JSON, often preferred in production environments for machine readability. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new CompactJsonFormatter()) .CreateLogger(); ``` -------------------------------- ### Using Rich Text Output Template Serilog Unity C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger with the Unity sink using a custom output template that incorporates Unity's Rich Text markup. This allows basic text styling (like bold, italic, color) in the log messages displayed in the Unity Console. Requires the Serilog.Sinks.Unity package. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(outputTemplate: "[{Level:u3}] {Message:lj}{NewLine}{Exception}"), .CreateLogger(); ``` -------------------------------- ### Adding Unity Object Context Manually via BeginScope (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Illustrates how to manually add a Unity object instance as the context property when using Microsoft.Extensions.Logging.Abstractions without UnityObjectLogger. This is achieved by using BeginScope with a dictionary or tuple containing the context property. Requires Microsoft.Extensions.Logging, a configured destructuring policy, and BeginScope. ```C# class MyLoggingType : UnityEngine.Object // MonoBehaviour, ScriptableObject, etc. { private ILogger _logger; private void Awake() { // Or Start(), or wherever your type sets up its logger _logger = _loggerFactory.CreateLogger(); } public void SomeMethod() { using (_logger.BeginScope(new Dictionary { { "@UnityLogContext", ValueTuple.Create(this) } })) _logger.LogInformation("Hello, world!"); // Or, when using Serilog.Extensions.Logging 9.0.0+ ... using (_logger.BeginScope(("@UnityLogContext", ValueTuple.Create(this)))) _logger.LogInformation("Hello, world!"); } } ``` -------------------------------- ### Adding Unity RemoteConfig with Custom Provider C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil.Configuration.RemoteConfig/README.md Demonstrates adding RemoteConfig as a configuration source using a proposed custom provider `AddUnityRemoteConfig`. This approach is currently problematic due to deadlocks or timing issues when fetching configurations asynchronously within the synchronous configuration build process. It includes conditional logic to apply this source only during gameplay or outside the editor. ```C# private async Task buildConfigurationAsync() { IConfigurationBuilder configurationBuilder = new ConfigurationBuilder(); // Add lower-priority configuration sources... if (!Application.isEditor || Application.isPlaying) { configurationBuilder.AddUnityRemoteConfig( new TUser(), new TApp(), remoteConfigInitializer: remoteCfg => { remoteCfg.SetEnvironmentID(/* ... */); // Init other RemoteConfig properties } ); } // Add higher-priority configuration sources... return configurationBuilder.Build(); } ``` -------------------------------- ### Creating Logger Extension Method - .NET Logging - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/UnityUtil/UnityUtil/README.md Defines a static C# class containing an extension method for ILogger. This method encapsulates a specific log message format and event ID, allowing for cleaner calling code and centralizing log message definitions. Uses nameof for event names and parameters. ```csharp // In LoggerExtensions.cs internal static class LoggerExtensions { public static void DoingStuff(this ILogger logger, int frameCount) { logger.LogInformation(new EventId(100, nameof(DoingStuff)), "Currently doing stuff in {frame}...", frameCount); } } ``` -------------------------------- ### Using LoggingLevelSwitch with Unity Sink Serilog C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger using the Unity sink with a LoggingLevelSwitch. This allows the minimum log level for this sink to be changed dynamically at runtime by updating the switch's Level property. Requires the Serilog.Sinks.Unity and Serilog.Core packages. ```C# LoggingLevelSwitch levelSwitch; var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(levelSwitch: levelSwitch) .CreateLogger(); ``` -------------------------------- ### Adding Unity Object Context Manually via LogContext (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Demonstrates manually adding a Unity object instance to the log event context using LogContext.PushProperty. The object must be wrapped in a ValueTuple and prefixed with '@' for proper destructuring by configured policies. Requires Serilog, LogContext, a ValueTuple, and a configured destructuring policy. ```C# class MyLoggingType : UnityEngine.Object // MonoBehaviour, ScriptableObject, etc. { private ILogger _logger; public void SomeMethod() { using (LogContext.PushProperty("@UnityLogContext", ValueTuple.Create(this))) _logger.Information("Hello, world!"); } } ``` -------------------------------- ### Configuring Serilog Unity Enricher with Settings (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Enrichers.Unity/README.md Illustrates how to customize the properties added by the Serilog Unity enricher using a UnityLogEnricherSettings object. This allows enabling specific data points, like unscaled time, and customising their log property names. Requires the Serilog library and the Serilog.Enrichers.Unity package. ```C# var logger = new Serilog.LoggerConfiguration() .Enrich.WithUnityData(new UnityLogEnricherSettings { WithUnscaledTime = true, UnscaledTimeLogProperty = "UT", }) // Configure other Serilog enrichers, sinks, etc. .CreateLogger(); ``` -------------------------------- ### Configuring Unity Context Property Name - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Shows how to customize the log scope property name used for the UnityEngine.Object instance context by providing a UnityObjectLoggerSettings object to the CreateLogger method and setting the UnityContextLogProperty. ```csharp _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { UnityContextLogProperty = "Context", }); ``` -------------------------------- ### Defining Custom Unity Logger - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows the definition of a simple custom class MyCustomLogger that inherits from UnityEngine.ILogger. This class serves as a placeholder to demonstrate how a user-defined Unity logger can be created and used with the Serilog Unity sink. Users must implement the ILogger interface methods. ```C# class MyCustomLogger : UnityEngine.ILogger { // ... } ``` -------------------------------- ### Creating ILogger with Unity Context - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Demonstrates how to obtain an ILogger instance for a UnityEngine.Object-derived type (like MonoBehaviour or ScriptableObject) using ILoggerFactory.CreateLogger(this). This automatically attaches the object instance as a log scope property, allowing logging providers to use it as the 'context' parameter for Unity Console output. ```csharp class MyLoggingType : UnityEngine.Object // MonoBehaviour, ScriptableObject, etc. { private ILogger _logger; private void Awake() { // Or Start(), or wherever your type sets up its logger _logger = _loggerFactory.CreateLogger(this); } public void SomeMethod() { _logger.LogInformation("Hello, world!"); // Automatically attaches `this` to the log message } } ``` -------------------------------- ### Configuring Serilog Destructuring for Unity Objects (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Adds a destructuring policy to the Serilog configuration that correctly handles UnityEngine.Object-derived instances, preventing default ToString() serialization and allowing sinks to use the actual object for Unity Console context. Requires Serilog configuration and the UnityObjectContext destructuring extension. ```C# var logger = new LoggerConfiguration() .Destructure.UnityObjectContext() // Configure other Serilog enrichers, sinks, etc. .CreateLogger() ``` -------------------------------- ### Adding Unity Object Context Automatically with UnityObjectLogger (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Shows how UnityObjectLogger from Unity.Extensions.Logging automatically adds the current Unity object instance as the context property when the logger is created with the object. This simplifies logging calls by removing the need for manual LogContext management. Requires Microsoft.Extensions.Logging, UnityObjectLogger, and a properly configured logger factory. ```C# class MyLoggingType : UnityEngine.Object // MonoBehaviour, ScriptableObject, etc. { private ILogger _logger; private void Awake() { // Or Start(), or wherever your type sets up its logger _logger = _loggerFactory.CreateLogger(this); } public void SomeMethod() { _logger.LogInformation("Hello, world!"); // Automatically adds log property for `this`, to be used as `context` in sinks } } ``` -------------------------------- ### Configuring Serilog Unity Sink with Context Property C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger to write to the Unity console using the Unity sink, explicitly setting the property name to look for the Unity context object. By default, this is "UnityLogContext". Requires the Serilog.Sinks.Unity package. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ UnityContextLogProperty = "UnityLogContext" }) .CreateLogger(); ``` -------------------------------- ### Enabling Serilog SelfLog for Unity Console (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Enables Serilog's internal error reporting mechanism (SelfLog) to direct output to the Unity Console, typically using Debug.LogError or Debug.LogWarning, to highlight configuration issues within the Serilog pipeline. Requires Serilog and Unity namespaces. ```C# SelfLog.Enable(UnityEngine.Debug.LogError); // Or .LogWarning() ``` -------------------------------- ### Adding Static Unity Property to Serilog (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Enrichers.Unity/README.md Demonstrates using Serilog's built-in Enrich.WithProperty method to add constant Unity values, such as the platform, to the logger configuration. This is suitable only for properties whose values do not change after the logger is configured. Requires the Serilog library and Unity Engine. ```C# var logger = new Serilog.LoggerConfiguration() .Enrich.WithProperty("Platform", Application.platform) // ... .CreateLogger(); ``` -------------------------------- ### Logging with Tag Pushed to LogContext - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates logging a message while pushing a specific property ("UnityLogTag") onto Serilog's LogContext. Because the sink is configured to read the tag from this property (as shown in the previous snippet), "TestTag" will be used as the Unity console tag for this specific log message. This provides a flexible way to assign tags based on execution context. ```C# using (LogContext.PushProperty("UnityLogTag", "TestTag")) logger.Information("Hello, world!"); ``` -------------------------------- ### Configuring Serilog Minimum Level from Default Unity Logger (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Configures the Serilog logger's minimum event level to match the filter log type of Unity's default logger (Debug.unityLogger), ensuring consistency between Unity's built-in logging filter and Serilog's output. Requires Serilog configuration and the IsUnityFilterLogType extension. ```C# var logger = new LoggerConfiguration() .MinimumLevel.IsUnityFilterLogType() // Configure other Serilog enrichers, sinks, etc. .CreateLogger() ``` -------------------------------- ### Configuring Serilog Minimum Level from Specific Unity ILogger (C#) Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Serilog/README.md Configures the Serilog logger's minimum event level to match the filter log type of a specified Unity ILogger instance, allowing custom loggers to control Serilog's output verbosity. Requires Serilog configuration, the IsUnityFilterLogType extension, and a valid ILogger instance. ```C# ILogger myLogger; var logger = new LoggerConfiguration() .MinimumLevel.IsUnityFilterLogType(myLogger) // Configure other Serilog enrichers, sinks, etc. .CreateLogger() ``` -------------------------------- ### Setting Minimum Log Level for Unity Sink Serilog C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger using the Unity sink, restricting the events processed by this sink to a specified minimum log level (e.g., Verbose). Events with a level lower than the configured minimum will be filtered out by the sink. Requires the Serilog.Sinks.Unity package and Serilog.Events. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(restrictedToMinimumLevel: LogEventLevel.Verbose) .CreateLogger(); ``` -------------------------------- ### Logging with Custom Tag Property - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates how to log a message using the Serilog logger configured in the previous step, which is set up to use the UnityLogTag property as the Unity console tag. It includes the UnityLogTag property directly in the log message template and provides its value ("TestTag"), which the sink will extract and use for the Unity log entry's tag parameter. ```C# logger.Information("Hello, my tag is '{UnityLogTag}'", "TestTag"); ``` -------------------------------- ### Configuring Serilog Unity Sink Tag From LogContext - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows how to configure Serilog to enrich logs from the LogContext and use a specific property ("UnityLogTag") from the context as the Unity log message's tag. It enables the Enrich.FromLogContext() enricher and sets the UnityTagLogProperty in the sink settings. This allows setting the tag dynamically using LogContext.PushProperty. ```C# var logger = new Serilog.LoggerConfiguration() .Enrich.FromLogContext() .WriteTo.Unity(new UnitySinkSettings{ UnityTagLogProperty = "UnityLogTag" }) .CreateLogger(); ``` -------------------------------- ### Creating Child Logger with SourceContext - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates creating a child Serilog logger from a root logger using ForContext(). This method automatically adds a SourceContext property to log events originating from this logger, with the value being the full name of MyLoggingType. When used with the default Unity sink configuration (which uses SourceContext as the tag property), this provides the type name as the Unity console tag. ```C# var logger = rootLogger.ForContext() ``` -------------------------------- ### Logging with Child Logger SourceContext Tag - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows a basic log message using a child logger created with ForContext(). Because this logger automatically adds the SourceContext property (representing the type T) and the default Unity sink configuration uses SourceContext as the tag, this log message will appear in the Unity console tagged with the full name of MyLoggingType. ```C# logger.Information("Hello, world!"); ``` -------------------------------- ### Configuring Serilog Unity Sink Custom Tag Property - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows how to configure the Serilog Unity sink to use a specific log event property as the Unity log message's tag. It creates a UnitySinkSettings object and sets its UnityTagLogProperty to "UnityLogTag". The sink will then look for a property with this name on incoming log events and use its value as the Unity tag, allowing for categorized logging in the Unity console. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ UnityTagLogProperty = "UnityLogTag" }) .CreateLogger(); ``` -------------------------------- ### Enabling Unity Hierarchy Name Logging - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Demonstrates how to configure an ILogger instance to include the UnityEngine.Object's hierarchy name (e.g., grandparent>parent>object) as a log scope property. This is achieved by setting AddHierarchyName to true in the UnityObjectLoggerSettings. ```csharp class MyLoggingType : UnityEngine.Object // MonoBehaviour, ScriptableObject, etc. { private ILogger _logger; private void Awake() { // Or Start(), or wherever your type sets up its logger _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { AddHierarchyName = true, }); } public void SomeMethod() { _logger.LogInformation("Hello, world!"); // Automatically attaches heirarchy name of `this` to the log message } } ``` -------------------------------- ### Configuring Hierarchy Name Separator - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Illustrates how to change the character used to separate parent names in the object's hierarchy name string. This is achieved by setting the ParentNameSeparator property in the UnityObjectLoggerSettings. ```csharp _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { AddHierarchyName = true, ParentNameSeparator = "-", }); ``` -------------------------------- ### Disabling Unity Log Context Lookup in Serilog C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger using the Unity sink, setting the UnityContextLogProperty to null. This prevents the sink from attempting to find and use a UnityEngine.Object instance from log event properties as the log message context. Requires the Serilog.Sinks.Unity package. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ UnityContextLogProperty = null }) .CreateLogger(); ``` -------------------------------- ### Disabling Unity Context Property - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Illustrates how to prevent the UnityEngine.Object instance from being automatically added as a log scope property by setting AddUnityContext to false in the UnityObjectLoggerSettings provided to the CreateLogger method. ```csharp _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { AddUnityContext = false, }); ``` -------------------------------- ### Indicating Non-Static Hierarchy - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Explains how to signal that an object's hierarchy may change during runtime, preventing the caching of its hierarchy name. This is done by setting the HasStaticHierarchy property to false in the UnityObjectLoggerSettings. ```csharp _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { AddHierarchyName = true, HasStaticHierarchy = false, }); ``` -------------------------------- ### Configuring Hierarchy Name Property Name - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Unity.Extensions.Logging/README.md Shows how to change the default log scope property name (UnityHierarchyName) used for the object's hierarchy name. This is done by setting the HierarchyNameLogProperty in the UnityObjectLoggerSettings. ```csharp _logger = _loggerFactory.CreateLogger(this, new UnityObjectLoggerSettings { AddHierarchyName = true, HierarchyNameLogProperty = "Name", }); ``` -------------------------------- ### Disabling Unity Tag Property Lookup - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet demonstrates how to configure the Serilog Unity sink to completely ignore the Unity log tag parameter. It achieves this by setting the UnityTagLogProperty property within the UnitySinkSettings object to null. When this is set, the sink will not attempt to find a log property to use as the Unity console tag. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ UnityTagLogProperty = null }) .CreateLogger(); ``` -------------------------------- ### Keeping Unity Context Property in Output Serilog C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md Configures a Serilog logger with the Unity sink, disabling the default behavior that removes the property used for the Unity context after processing. This allows the context property (or its serialized value) to remain in the log event for formatters. Requires the Serilog.Sinks.Unity package. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ RemoveUnityContextLogPropertyIfPresent = false }) .CreateLogger(); ``` -------------------------------- ### Keeping Tag Property in Formatted Message - C# Source: https://github.com/derploidentertainment/unityutil/blob/main/src/Logging/Serilog.Sinks.Unity/README.md This C# snippet shows how to prevent the Serilog Unity sink from removing the property used for the Unity log tag from the log event before formatting. By setting RemoveUnityTagLogPropertyIfPresent to false in the UnitySinkSettings, the property will remain in the log event data and can be included in the formatted output by the configured formatter. ```C# var logger = new Serilog.LoggerConfiguration() .WriteTo.Unity(new UnitySinkSettings{ RemoveUnityTagLogPropertyIfPresent = false }) .CreateLogger(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.