Step 1 Download the latest version of log4net library
Step 2 Add log4net reference to your project
Step 3 Add log4net assembly to GAC
Step 4 Select your SharePoint web application. E.g.
C:\inetpub\wwwroot\wss\VirtualDirectories\88
Choose one of following steps (Step 5, Step 6):
Step 5 Add below line in your AssemblyInfo.cs file.
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "C:\\inetpub\\wwwroot\\wss\\VirtualDirectories\\88\\web.config", Watch = true)]
Step 6 Modify your SharePoint web application’s global.asax
C:\inetpub\wwwroot\wss\VirtualDirectories\88\global.asax
<%@ Assembly Name="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" %>
<%@ Import Namespace="log4net" %>
<%@ Import Namespace="log4net.Config" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
void Application_Start(object sender, EventArgs e)
{
//string currentFolder = HttpContext.Current.Request.PhysicalApplicationPath;//Context.Request.PhysicalApplicationPath;
//log4net.Config.XmlConfigurator.Configure(new FileInfo(currentFolder + "web.config"));
log4net.Config.XmlConfigurator.Configure(new FileInfo("C:\\inetpub\\wwwroot\\wss\\VirtualDirectories\\88\\web.config"));
}
</script>
Step 7 Add below sections in your SharePoint web application’s web.config
C:\inetpub\wwwroot\wss\VirtualDirectories\88\web.config
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
…
</configSections>
…
<log4net debug="true">
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="[set your file path log here]" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="10MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
<root>
<level value="ALL" />
<appender-ref ref="RollingLogFileAppender" />
</root>
</log4net>
</configuration>
Snippet code using log4net in SharePoint Webpart
[Guid("857bb65a-fd9c-4294-9ddf-7f090d777267")]
public class log4netWebPartTest : Microsoft.SharePoint.WebPartPages.WebPart
{
#region log4net
private static readonly ILog _log = LogManager.GetLogger(typeof(log4netWebPartTest).Name);
#endregion
private bool _error = false;
private string _myProperty = null;
[Personalizable(PersonalizationScope.Shared)]
[WebBrowsable(true)]
[System.ComponentModel.Category("My Property Group")]
[WebDisplayName("MyProperty")]
[WebDescription("Meaningless Property")]
public string MyProperty
{
get
{
if (_myProperty == null)
{
_myProperty = "Hello SharePoint";
}
return _myProperty;
}
set { _myProperty = value; }
}
public log4netWebPartTest()
{
this.ExportMode = WebPartExportMode.All;
}
/// <summary>
/// Create all your controls here for rendering.
/// Try to avoid using the RenderWebPart() method.
/// </summary>
protected override void CreateChildControls()
{
if (!_error)
{
try
{
base.CreateChildControls();
// Your code here...
this.Controls.Add(new LiteralControl(this.MyProperty));
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
/// <summary>
/// Ensures that the CreateChildControls() is called before events.
/// Use CreateChildControls() to create your controls.
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
if (!_error)
{
try
{
base.OnLoad(e);
this.EnsureChildControls();
try
{
File.ReadAllBytes(@"fileDoesNotExist.txt");
}
catch (FileNotFoundException ex)
{
if (_log.IsErrorEnabled)
{
_log.Error("OnLoad error: " + ex.Message);
}
}
}
catch (Exception ex)
{
HandleException(ex);
}
}
}
/// <summary>
/// Clear all child controls and add an error message for display.
/// </summary>
/// <param name="ex"></param>
private void HandleException(Exception ex)
{
this._error = true;
this.Controls.Clear();
this.Controls.Add(new LiteralControl(ex.Message));
}
}
0 comments :
Post a Comment