问题
So I'm writing a WebApi using C#, I configured log4net in my web.config (used log4net.azure) my configuration is the following:
<appender name="AzureTableAppender" type="log4net.Appender.AzureTableAppender, log4net.Appender.Azure">
<param name="TableName" value="myLogs" />
<param name="ConnectionStringName" value="AzureTable" />
<param name="PropAsColumn" value="true" />
<bufferSize value="1" />
<param name="PartitionKeyType" value="LoggerName" />
</appender>
My issue is that I'm getting a lot of extra columns that I don't need, for now I'm getting the following:
I'm searching for a way to be able to pick which columns to be displayed and maybe add more columns but by code without going to my azure account and untick what I don't want.
回答1:
Yes, you can control the columns to be displayed or not using code.
For example, assume that you wanna only have the 4 columns: Message / Level / RoleInstance / DeploymentId in your azure table storage, you can follow the steps:
Step 1.Create a table contanis the above 4 columns:
public class AzureLog : TableEntity
{
public AzureLog()
{
var now = DateTime.UtcNow;
PartitionKey = string.Format("{0:yyyy-MM}", now);
RowKey = string.Format("{0:dd HH:mm:ss.fff}-{1}", now, Guid.NewGuid());
}
public string RoleInstance { get; set; }
public string DeploymentId { get; set; }
public string Message { get; set; }
public string Level { get; set; }
}
Step 2.Create a class, just use to create the table where stored your log. Here we named it Logger:
public class Logger
{
//Storage credentials.
public StorageCredentials credentials = null;
//Storage account.
public CloudStorageAccount account = null;
//Table client
public CloudTableClient tableClient = null;
//Table.
public CloudTable table = null;
// Constructor.
public Logger(string tableName, string accountName, string accountKey)
{
//Create storage credentials object.
credentials = new StorageCredentials(accountName,
accountKey);
//Create storage account object.
account = new CloudStorageAccount(credentials, false);
//Create table client object.
tableClient = account.CreateCloudTableClient();
//Get the table reference.
table = tableClient.GetTableReference(tableName);
//Check whether table exist or not.
if (!table.Exists())
{
//Create the table if not exist.
table.Create();
}
}
// Insert into table.
public void Insert(AzureLog objAzureLog)
{
// Create the new insert table operation.
TableOperation insertOperation = TableOperation.Insert(objAzureLog);
// Execute the insert statement.
table.Execute(insertOperation);
}
}
Step 3.define your custom appender:
public class AzureLogAppender : AppenderSkeleton
{
//Logger object.
private Logger objLogger = null;
//Azure table name.
public string tableName { get; set; }
//Azure account name.
public string accountName { get; set; }
//Azure account key.
public string accountKey { get; set; }
public override void ActivateOptions()
{
base.ActivateOptions();
//Logger object.
if (objLogger == null)
{
//Intilize logger object.
this.objLogger = new Logger(tableName, accountName, accountKey);
}
}
protected override void Append(LoggingEvent loggingEvent)
{
try
{
//Intilize AzureLog object.
AzureLog objAzureLog = new AzureLog
{
RoleInstance = "1",
DeploymentId = "100",
Message = loggingEvent.RenderedMessage,
Level = loggingEvent.Level.Name,
};
//Insert the log.
objLogger.Insert(objAzureLog);
}
catch (Exception ex)
{
//Handle exception here.
}
}
}
Step 4.Test the code:
class Program
{
//Get the logger object.
private static readonly ILog logger = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
try
{
logger.Debug("Debug information.");
logger.Info("Info information.");
logger.Warn("Warn information.");
logger.Error("Error information.");
logger.Fatal("Fatal information.");
Console.WriteLine("ok");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
}
Step 5.My app.config as below:
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<appender name="AzureLogAppender" type="Log4NetAzureTables.AzureLogAppender, Log4NetAzureTables" >
<param name="tableName" value="your table name" />
<param name="accountName" value="your account" />
<param name="accountKey" value="your account key" />
</appender>
<root>
<level value="ALL" />
<appender-ref ref="AzureLogAppender" />
</root>
</log4net>
</configuration>
Step 6.Nav to table, only the specified columns are added:
来源:https://stackoverflow.com/questions/51841879/is-it-possible-to-customize-azure-table-storage-by-code-log4net-azure