namespace MyProject.Core
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Web;
using log4net.Appender;
using log4net.spi;
/// <summary>
/// Custom log file appender
/// </summary>
public class CustomLogFileAppender : FileAppender
{
/// <summary>
/// The m_current date
/// </summary>
private DateTime m_currentDate;
/// <summary>
/// The m_original file name
/// </summary>
private string m_originalFileName;
/// <summary>
/// Initializes a new instance of the <see cref="CustomLogFileAppender"/> class.
/// </summary>
public CustomLogFileAppender()
{
this.m_currentDate = DateTime.Now;
}
/// <summary>
/// Gets or sets the file.
/// </summary>
/// <value>
/// The file.
/// </value>
public override string File
{
get
{
return base.File;
}
set
{
if (this.m_originalFileName == null)
{
string str = value;
Dictionary<string, string> variables = ConfigReader.GetVariables();
foreach (string index in variables.Keys)
{
string oldValue = "$(" + index + ")";
str = str.Replace(oldValue, variables[index]);
}
this.m_originalFileName = this.MapPath(str.Trim());
}
base.File = this.m_originalFileName;
}
}
/// <summary>
/// Makes the path.
/// </summary>
/// <param name="part1">The part1.</param>
/// <param name="part2">The part2.</param>
/// <param name="separator">The separator.</param>
/// <returns>
/// Complete path.
/// </returns>
public static string MakePath(string part1, string part2, char separator)
{
if (string.IsNullOrEmpty(part1))
{
return part2 ?? string.Empty;
}
if (string.IsNullOrEmpty(part2))
{
return part1 ?? string.Empty;
}
if ((int)part1[part1.Length - 1] == (int)separator)
{
part1 = part1.Substring(0, part1.Length - 1);
}
if ((int)part2[0] == (int)separator)
{
part2 = part2.Substring(1);
}
return part1 + (object)separator + part2;
}
/// <summary>
/// Appends the specified logging event.
/// </summary>
/// <param name="loggingEvent">The logging event.</param>
protected override void Append(LoggingEvent loggingEvent)
{
DateTime now = DateTime.Now;
if (this.m_currentDate.Day != now.Day || this.m_currentDate.Month != now.Month || this.m_currentDate.Year != now.Year)
{
lock (this)
{
this.CloseFile();
this.m_currentDate = DateTime.Now;
this.OpenFile(string.Empty, false);
}
}
base.Append(loggingEvent);
}
/// <summary>
/// Opens the file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <param name="append">if set to <c>true</c> [append].</param>
protected override void OpenFile(string fileName, bool append)
{
fileName = this.m_originalFileName;
fileName = fileName.Replace("{date}", this.m_currentDate.ToString("yyyyMMdd"));
fileName = fileName.Replace("{time}", this.m_currentDate.ToString("HHmmss"));
fileName = fileName.Replace("{processid}", CustomLogFileAppender.GetCurrentProcessId().ToString());
base.OpenFile(fileName, append);
}
/// <summary>
/// Gets the current process identifier.
/// </summary>
/// <returns>
/// The process Id.
/// </returns>
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int GetCurrentProcessId();
/// <summary>
/// Gets the name of the timed file.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>
/// Filename with timestamp.
/// </returns>
private string GetTimedFileName(string fileName)
{
int num = fileName.LastIndexOf('.');
if (num < 0)
{
return fileName;
}
return
string.Concat(
new object[4]
{
(object)fileName.Substring(0, num), (object)'.', (object)this.m_currentDate.ToString("HHmmss"),
(object)fileName.Substring(num)
});
}
/// <summary>
/// Determines whether the specified file name is locked.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>
/// Locked or not.
/// </returns>
private bool IsLocked(string fileName)
{
if (!System.IO.File.Exists(fileName))
{
return false;
}
try
{
FileStream fileStream = System.IO.File.OpenWrite(fileName);
if (fileStream == null)
{
return true;
}
fileStream.Close();
return false;
}
catch (Exception ex)
{
string message = ex.Message;
return true;
}
}
/// <summary>
/// Maps the path.
/// </summary>
/// <param name="fileName">Name of the file.</param>
/// <returns>
/// Mapped path.
/// </returns>
private string MapPath(string fileName)
{
if (fileName == string.Empty || fileName.IndexOf(":/", System.StringComparison.Ordinal) >= 0 || fileName.IndexOf("://", System.StringComparison.Ordinal) >= 0)
{
return fileName;
}
var index = fileName.IndexOfAny(new char[2] { '\\', '/' });
if (index >= 0 && (int)fileName[index] == 92)
{
return fileName.Replace('/', '\\');
}
fileName = fileName.Replace('\\', '/');
if (HttpContext.Current != null)
{
return HttpContext.Current.Server.MapPath(fileName);
}
return (int)fileName[0] == 47 ? SitecoreLogFileAppender.MakePath(HttpRuntime.AppDomainAppPath, fileName.Replace('/', '\\'), '\\') : fileName;
}
}
}