Class UnityLogService
Abstract base class for Unity-based logging services that implement ILogService.
Extends MonoBehaviour for proper Unity lifecycle integration.
Provides logging functionality with configurable verbosity levels, category overrides, and message formatting.
Namespace: Serenity.Logging.Infrastructure.Services
Assembly: Serenity.UnityGlobal.Infrastructure.dll
Syntax
public abstract class UnityLogService : MonoBehaviour, ILogService, IService, IFoundationSettings
Constructors
UnityLogService()
Declaration
protected UnityLogService()
Fields
_overrides
Declaration
protected Dictionary<string, LogVerbosity> _overrides
Field Value
| Type | Description |
|---|---|
| Dictionary<string, LogVerbosity> | Dictionary of category-specific verbosity overrides for fine-grained logging control. |
_settings
Declaration
protected ILogSettings _settings
Field Value
| Type | Description |
|---|---|
| ILogSettings | The logging settings configuration used by this service. |
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Verbosity
Gets or sets the global verbosity level for this logging service.
Declaration
public LogVerbosity Verbosity { get; set; }
Property Value
| Type | Description |
|---|---|
| LogVerbosity | The global verbosity level for this logging service. |
Methods
ClearCategoryVerbosity(string)
Clears any verbosity override for the specified category.
After calling this method, the category will use the global verbosity level.
Declaration
public void ClearCategoryVerbosity(string category)
Parameters
| Type | Name | Description |
|---|---|---|
| string | category | The category to clear the override for (ignored if null or whitespace). |
For(string)
Creates a component logger instance for the specified category.
The component logger provides category-specific logging operations using this service.
Declaration
public ILoggerComponent For(string category)
Parameters
| Type | Name | Description |
|---|---|---|
| string | category | The category name for the component logger. |
Returns
| Type | Description |
|---|---|
| ILoggerComponent | An IComponentLogger instance configured for the specified category. |
Format(LogEntry, bool)
Formats a log entry into a readable string format using the configured settings.
Includes timestamps, severity level, category, tags, and message based on service configuration.
Declaration
protected virtual string Format(LogEntry e, bool prefixOnly = false)
Parameters
| Type | Name | Description |
|---|---|---|
| LogEntry | e | The log entry to format. |
| bool | prefixOnly | If true, returns only the prefix portion without the message content. |
Returns
| Type | Description |
|---|---|
| string | A formatted string representation of the log entry. |
GetEffectiveVerbosity(string)
Gets the effective verbosity level for a specific category.
Returns the category-specific override if available, otherwise returns the global verbosity.
Declaration
public LogVerbosity GetEffectiveVerbosity(string category)
Parameters
| Type | Name | Description |
|---|---|---|
| string | category | The category to check the verbosity for. |
Returns
| Type | Description |
|---|---|
| LogVerbosity | The effective LogVerbosity level for the specified category. |
Initialize(CreateLogServiceDTO)
Initializes the UnityLogService with the specified configuration.
Sets up verbosity levels and category-specific overrides from the provided settings.
Declaration
public virtual void Initialize(CreateLogServiceDTO input)
Parameters
| Type | Name | Description |
|---|---|---|
| CreateLogServiceDTO | input | The DTO containing logging service configuration including settings. |
IsEnabledFor(string, LogSeverity)
Determines if logging is enabled for the specified category and severity level.
Uses effective verbosity level to filter log messages based on their severity.
Declaration
public bool IsEnabledFor(string category, LogSeverity severity)
Parameters
| Type | Name | Description |
|---|---|---|
| string | category | The category to check. |
| LogSeverity | severity | The severity level to check. |
Returns
| Type | Description |
|---|---|
| bool | True if logging is enabled for the specified category and severity, false otherwise. |
SetCategoryVerbosity(string, LogVerbosity?)
Sets the verbosity level for a specific category.
If verbosity is null, removes any existing override for the category.
Declaration
public void SetCategoryVerbosity(string category, LogVerbosity? verbosity)
Parameters
| Type | Name | Description |
|---|---|---|
| string | category | The category to set the verbosity for (ignored if null or whitespace). |
| LogVerbosity? | verbosity | The verbosity level to set, or null to remove the override. |
WriteErrorLog(string, string, params string[])
Writes an error-level log message with the specified content and optional metadata.
Declaration
public void WriteErrorLog(string message, string category = null, params string[] tags)
Parameters
| Type | Name | Description |
|---|---|---|
| string | message | The message to log. |
| string | category | Optional category for the log message. |
| string[] | tags | Optional array of tags to associate with the log entry. |
WriteExceptionLog(Exception, string, string, params string[])
Writes an exception-level log message with the specified exception and optional metadata.
Uses the exception message if no custom message is provided.
Declaration
public void WriteExceptionLog(Exception ex, string message = null, string category = null, params string[] tags)
Parameters
| Type | Name | Description |
|---|---|---|
| Exception | ex | The exception to log. |
| string | message | Optional custom message, defaults to exception message if null. |
| string | category | Optional category for the log message. |
| string[] | tags | Optional array of tags to associate with the log entry. |
WriteInformationLog(string, string, params string[])
Writes an information-level log message with the specified content and optional metadata.
Declaration
public void WriteInformationLog(string message, string category = null, params string[] tags)
Parameters
| Type | Name | Description |
|---|---|---|
| string | message | The message to log. |
| string | category | Optional category for the log message. |
| string[] | tags | Optional array of tags to associate with the log entry. |
WriteLog(LogEntry)
Abstract method that concrete implementations must override to handle actual log writing.
This method is called by all other logging methods to perform the actual log output.
Declaration
public abstract void WriteLog(LogEntry e)
Parameters
| Type | Name | Description |
|---|---|---|
| LogEntry | e | The log entry containing all information to be logged. |
WriteVerboseLog(string, string, params string[])
Writes a verbose-level log message with the specified content and optional metadata.
Declaration
public void WriteVerboseLog(string message, string category = null, params string[] tags)
Parameters
| Type | Name | Description |
|---|---|---|
| string | message | The message to log. |
| string | category | Optional category for the log message. |
| string[] | tags | Optional array of tags to associate with the log entry. |
WriteWarningLog(string, string, params string[])
Writes a warning-level log message with the specified content and optional metadata.
Declaration
public void WriteWarningLog(string message, string category = null, params string[] tags)
Parameters
| Type | Name | Description |
|---|---|---|
| string | message | The message to log. |
| string | category | Optional category for the log message. |
| string[] | tags | Optional array of tags to associate with the log entry. |