Created
August 16, 2018 10:11
-
-
Save crnastena/cfab084ee196cd18ce4e2b761b7d48bb to your computer and use it in GitHub Desktop.
When using RedisSessionStateProvider and/or RedisOutputCacheProvider, and want to specify loggingClassName and loggingMethodName to write to log4Net.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Text; | |
using log4net; | |
namespace RedisUtil | |
{ | |
public class Log4NetTextWriter : TextWriter | |
{ | |
private static readonly ILog Log = LogManager.GetLogger(typeof(Log4NetTextWriter)); | |
public override Encoding Encoding => Encoding.UTF8; | |
#region properties | |
private StringBuilder Buffer { get; } | |
private string LogPrefix { get; } | |
#endregion properties ; | |
public override void Flush() | |
{ | |
if (Buffer != null && Buffer.Length > 0) | |
{ | |
WriteLine(); | |
} | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
Flush(); | |
base.Dispose(disposing); | |
} | |
#region public constructors | |
public Log4NetTextWriter(string logPrefix = null) : this(null, logPrefix) | |
{ | |
} | |
public Log4NetTextWriter(IFormatProvider formatProvider, string logPrefix) : base(formatProvider) | |
{ | |
Buffer = new StringBuilder(); | |
LogPrefix = logPrefix; | |
} | |
#endregion public constructors | |
#region public Write() overloads | |
public override void Write(bool value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(char value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(char[] buffer) | |
{ | |
Buffer.Append(buffer); | |
} | |
public override void Write(char[] buffer, int index, int count) | |
{ | |
Buffer.Append(buffer, index, count); | |
} | |
public override void Write(decimal value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(double value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(float value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(int value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(long value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(object value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(string format, object arg0) | |
{ | |
Buffer.AppendFormat(FormatProvider, format, arg0); | |
} | |
public override void Write(string format, object arg0, object arg1) | |
{ | |
Buffer.AppendFormat(FormatProvider, format, arg0, arg1); | |
} | |
public override void Write(string format, object arg0, object arg1, object arg2) | |
{ | |
Buffer.AppendFormat(FormatProvider, format, arg0, arg1, arg2); | |
} | |
public override void Write(string format, params object[] arg) | |
{ | |
Buffer.AppendFormat(FormatProvider, format, arg); | |
} | |
public override void Write(string value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(uint value) | |
{ | |
Buffer.Append(value); | |
} | |
public override void Write(ulong value) | |
{ | |
Buffer.Append(value); | |
} | |
#endregion public Write() overloads | |
#region public WriteLine() overloads | |
public override void WriteLine() | |
{ | |
string logMessage = Buffer.ToString(); | |
Buffer.Length = 0; | |
var tempLogPrefix = string.IsNullOrEmpty(LogPrefix) ? "" : $"{LogPrefix}() --> "; | |
Log.Info($"{tempLogPrefix}{logMessage}"); | |
} | |
public override void WriteLine(bool value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(char value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(char[] buffer) | |
{ | |
Write(buffer); | |
WriteLine(); | |
} | |
public override void WriteLine(char[] buffer, int index, int count) | |
{ | |
Write(buffer, index, count); | |
WriteLine(); | |
} | |
public override void WriteLine(decimal value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(double value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(float value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(int value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(long value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(object value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(string format, object arg0) | |
{ | |
Write(format, arg0); | |
WriteLine(); | |
} | |
public override void WriteLine(string format, object arg0, object arg1) | |
{ | |
Write(format, arg0, arg1); | |
WriteLine(); | |
} | |
public override void WriteLine(string format, object arg0, object arg1, object arg2) | |
{ | |
Write(format, arg0, arg1, arg2); | |
WriteLine(); | |
} | |
public override void WriteLine(string format, params object[] arg) | |
{ | |
Write(format, arg); | |
WriteLine(); | |
} | |
public override void WriteLine(string value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(uint value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
public override void WriteLine(ulong value) | |
{ | |
Write(value); | |
WriteLine(); | |
} | |
#endregion public WriteLine() overloads | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO; | |
namespace RedisUtil | |
{ | |
public class RedisLogger | |
{ | |
public static TextWriter LogSessionStateActivity() | |
{ | |
return new Log4NetTextWriter("REDIS-SESSION-STATE"); | |
} | |
public static TextWriter LogOutputCacheActivity() | |
{ | |
return new Log4NetTextWriter("REDIS-OUTPUT-CACHE"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<system.web> | |
<sessionState mode="Custom" customProvider="MySessionStateStore"> | |
<providers> | |
<add name="MySessionStateStore" | |
type="Microsoft.Web.Redis.RedisSessionStateProvider" | |
databaseId="0" | |
applicationName="REDIS-SESSION" | |
retryTimeoutInMilliseconds="10000" | |
connectionTimeoutInMilliseconds="10000" | |
operationTimeoutInMilliseconds="2000" | |
connectionString="{name}.redis.cache.windows.net:6380,password={accessKey},ssl=True,abortConnect=False" | |
loggingClassName="RedisUtil.RedisLogger" | |
loggingMethodName="LogSessionStateActivity" /> | |
</providers> | |
</sessionState> | |
<caching> | |
<outputCache defaultProvider="MyRedisOutputCache"> | |
<providers> | |
<add name="MyRedisOutputCache" | |
type="Microsoft.Web.Redis.RedisOutputCacheProvider" | |
databaseId="1" | |
applicationName="REDIS-CACHE" | |
connectionString="{name}.redis.cache.windows.net:6380,password={accessKey},ssl=True,abortConnect=False" | |
loggingClassName="RedisUtil.RedisLogger" | |
loggingMethodName="LogOutputCacheActivity" /> | |
</providers> | |
</outputCache> | |
</caching> | |
</system.web> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment