Note:
This documentation is for version 4.4.0 of the library.
Some samples may not work with other versions.
Installation
Install the Google.Cloud.Logging.Log4Net package from NuGet. Add it to
your project in the normal way (for example by right-clicking on the
project in Visual Studio and choosing "Manage NuGet Packages...").
Authentication
When running on Google Cloud, no action needs to be taken to authenticate.
Otherwise, the simplest way of authenticating your API calls is to
set up Application Default Credentials.
The credentials will automatically be used to authenticate. See
Set up Application Default Credentials for more details.
Alternatively, credentials can be provided when configuring
GoogleStackdriverAppender. See the configuration guide
for details, specifically the CredentialFile and CredentialJson
options.
Getting started
See the configuration guide for details of all
GoogleStackdriverAppender configuration options.
Edit the file replacing PROJECT_ID with your Google Cloud Project
ID, and LOG_ID with an identifier for your application.
Use this file to configure log4net and then log as normal:
// Configure log4net to use Google Cloud Logging from the XML configuration file.
XmlConfigurator.Configure(LogManager.GetRepository(GetType().Assembly), new FileInfo("log4net.xml"));
// Retrieve a logger for this context.
ILog log = LogManager.GetLogger(typeof(Program));
// Log some information. This log entry will be sent to Google Cloud Logging.
log.Info("An exciting log entry!");
// Flush buffered log entries before program exit.
// This is required because log entries are buffered locally before being sent to Google Cloud Logging.
// LogManager.Flush() only works in the full .NET framework (not in .NET Core):
bool flushCompleted = LogManager.Flush(10_000);
// On .NET Core, the specific repository needs to be flushed:
bool repositoryFlushCompleted = ((IFlushable)LogManager.GetRepository(GetType().Assembly)).Flush(10_000);
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[[["\u003cp\u003e\u003ccode\u003eGoogle.Cloud.Logging.Log4Net\u003c/code\u003e is a .NET client library that integrates Google Cloud Logging with log4net.\u003c/p\u003e\n"],["\u003cp\u003eThe library's latest version is 4.4.0, and documentation is provided for this version, with some samples not working in others.\u003c/p\u003e\n"],["\u003cp\u003eAuthentication is automatic on Google Cloud, but for other environments, Application Default Credentials can be used or credentials can be specified in the \u003ccode\u003eGoogleStackdriverAppender\u003c/code\u003e configuration.\u003c/p\u003e\n"],["\u003cp\u003eFor ASP.NET and console applications, configuration files (Web.config or log4net.xml) are used to define the \u003ccode\u003eGoogleStackdriverAppender\u003c/code\u003e, specifying the project ID and log ID, which are auto-detected in certain Google Cloud environments.\u003c/p\u003e\n"],["\u003cp\u003eBuffered log entries need to be flushed before exiting the program to ensure they are sent to Google Cloud Logging, using \u003ccode\u003eLogManager.Flush()\u003c/code\u003e for the full .NET framework, and for .NET core you need to flush the specific repository.\u003c/p\u003e\n"]]],[],null,["Version latestkeyboard_arrow_down\n\n- [4.4.0 (latest)](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/latest)\n- [4.3.0](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/4.3.0)\n- [4.2.0](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/4.2.0)\n- [4.1.0](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/4.1.0)\n- [4.0.0](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/4.0.0)\n- [3.3.0](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/3.3.0) \n\nGoogle.Cloud.Logging.Log4Net\n============================\n\n`Google.Cloud.Logging.Log4Net` is a .NET client library to integrate [Google Cloud\nLogging](https://cloud.google.com/logging/) with\n[log4net](https://logging.apache.org/log4net/).\n\nNote:\nThis documentation is for version `4.4.0` of the library.\nSome samples may not work with other versions.\n\nInstallation\n------------\n\nInstall the `Google.Cloud.Logging.Log4Net` package from NuGet. Add it to\nyour project in the normal way (for example by right-clicking on the\nproject in Visual Studio and choosing \"Manage NuGet Packages...\").\n\nAuthentication\n--------------\n\nWhen running on Google Cloud, no action needs to be taken to authenticate.\n\nOtherwise, the simplest way of authenticating your API calls is to\nset up Application Default Credentials.\nThe credentials will automatically be used to authenticate. See\n[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc) for more details.\n\nAlternatively, credentials can be provided when configuring\n`GoogleStackdriverAppender`. See the [configuration guide](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/latest/configuration)\nfor details, specifically the `CredentialFile` and `CredentialJson`\noptions.\n\nGetting started\n---------------\n\nSee the [configuration guide](/dotnet/docs/reference/Google.Cloud.Logging.Log4Net/latest/configuration) for details of all\n`GoogleStackdriverAppender` configuration options.\n\nASP.NET\n-------\n\nAdd the log4net section to the `Web.config` file: \n\n \u003cconfiguration\u003e\n \u003cconfigSections\u003e\n\n \u003c!-- Other configSection item --\u003e\n\n \u003c!-- Specify a configuration section for log4net --\u003e\n \u003csection name=\"log4net\" type=\"log4net.Config.Log4NetConfigurationSectionHandler,log4net\" /\u003e\n \u003c/configSections\u003e\n\n \u003c!-- The log4net configuration --\u003e\n \u003clog4net\u003e\n \u003cappender name=\"CloudLogger\" type=\"Google.Cloud.Logging.Log4Net.GoogleStackdriverAppender,Google.Cloud.Logging.Log4Net\"\u003e\n \u003clayout type=\"log4net.Layout.PatternLayout\"\u003e\n \u003cconversionPattern value=\"%-4timestamp [%thread] %-5level %logger %ndc - %message\" /\u003e\n \u003c/layout\u003e\n \u003cprojectId value=\"PROJECT_ID\" /\u003e\n \u003clogId value=\"LOG_ID\" /\u003e\n \u003c/appender\u003e\n \u003croot\u003e\n \u003clevel value=\"ALL\" /\u003e\n \u003cappender-ref ref=\"CloudLogger\" /\u003e\n \u003c/root\u003e\n \u003c/log4net\u003e\n\n \u003c!-- Other configuration items --\u003e\n\n \u003c/configuration\u003e\n\nEdit the file replacing `PROJECT_ID` with your Google Cloud Project\nID, and `LOG_ID` with an identifier for your application. \n\n // Load log4net configuration from Web.config\n log4net.Config.XmlConfigurator.Configure(LogManager.GetRepository(GetType().Assembly));\n\nIf executing on [Google App Engine (GAE)](https://cloud.google.com/appengine/),\n[Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/),\nor [Google Compute Engine (GCE)](https://cloud.google.com/compute/),\nthen the `\u003cprojectId value=\"PROJECT_ID\" /\u003e` configuration setting can be omitted; it will be auto-detected from the platform at run-time.\n\nConsole app\n-----------\n\nCreate a `log4net` configuration file (`log4net.xml`): \n\n \u003c?xml version=\"1.0\" encoding=\"utf-8\" ?\u003e\n \u003clog4net\u003e\n \u003cappender name=\"CloudLogger\" type=\"Google.Cloud.Logging.Log4Net.GoogleStackdriverAppender,Google.Cloud.Logging.Log4Net\"\u003e\n \u003clayout type=\"log4net.Layout.PatternLayout\"\u003e\n \u003cconversionPattern value=\"%-4timestamp [%thread] %-5level %logger %ndc - %message\" /\u003e\n \u003c/layout\u003e\n \u003cprojectId value=\"PROJECT_ID\" /\u003e\n \u003clogId value=\"LOG_ID\" /\u003e\n \u003c/appender\u003e\n \u003croot\u003e\n \u003clevel value=\"ALL\" /\u003e\n \u003cappender-ref ref=\"CloudLogger\" /\u003e\n \u003c/root\u003e\n \u003c/log4net\u003e\n\nEdit the file replacing `PROJECT_ID` with your Google Cloud Project\nID, and `LOG_ID` with an identifier for your application.\n\nUse this file to configure `log4net` and then log as normal: \n\n // Configure log4net to use Google Cloud Logging from the XML configuration file.\n XmlConfigurator.Configure(LogManager.GetRepository(GetType().Assembly), new FileInfo(\"log4net.xml\"));\n\n // Retrieve a logger for this context.\n ILog log = LogManager.GetLogger(typeof(Program));\n // Log some information. This log entry will be sent to Google Cloud Logging.\n log.Info(\"An exciting log entry!\");\n\n // Flush buffered log entries before program exit.\n // This is required because log entries are buffered locally before being sent to Google Cloud Logging.\n // LogManager.Flush() only works in the full .NET framework (not in .NET Core):\n bool flushCompleted = LogManager.Flush(10_000);\n // On .NET Core, the specific repository needs to be flushed:\n bool repositoryFlushCompleted = ((IFlushable)LogManager.GetRepository(GetType().Assembly)).Flush(10_000);\n\nIf executing on [Google App Engine (GAE)](https://cloud.google.com/appengine/),\n[Google Kubernetes Engine (GKE)](https://cloud.google.com/kubernetes-engine/),\nor [Google Compute Engine (GCE)](https://cloud.google.com/compute/),\nthen the `\u003cprojectId value=\"PROJECT_ID\" /\u003e` configuration setting can be omitted; it will be auto-detected from the platform at run-time."]]